首页 > 解决方案 > 错误:“void”类型上不存在属性“then”

问题描述

当用户想要登录移动应用程序时,我试图从 Firebase 实时数据库中获取一些用户状态信息(isDeleted:true/false)。在尝试这个时,我得到了这个错误:

“void”类型上不存在属性“then”

代码如下所示:

登录页面.ts

  loginUser() {
    const data = this.loginForm.value;
    if (!data.userName) {
      return;
    }

    this.loadingCtrl.create({
      message: 'Loading...'
    }).then((res) => {
      res.present();
    });

    const credentials = {
      email: data.userName + this.username_domain,
      password: data.password
    };

    this.authService.loginUser(credentials)
      .then(res => {
        this.userService.setOrgId(res.user.photoURL);
        this.loadingCtrl.dismiss();
        this.router.navigateByUrl('/tabs');

        //The error occours here.
        this.userService.getUserStatus().then((isDeleted: any) => {
          console.log('isDeleted ', isDeleted)
          if (isDeleted) {
            err => {
              this.loadingCtrl.dismiss();
              this.toast = this.toastCtrl.create({
                message: 'Your account is not active anymore.',
                duration: 2000
                  });
              } 
              this.authService.logoutUser()
              .then(async (res) => {
                this.clearLocalData();
                this.navCtrl.navigateRoot('login');
              })
              .catch(error => {
                console.log(error);
              });
          }
        });

      }, err => {
        this.loadingCtrl.dismiss();
        this.toast = this.toastCtrl.create({
          message: err.message,
          duration: 2000
        }).then((toastData) => {
          toastData.present();
        });
      });
    }

用户服务.ts

getUserStatus() {
   firebase.database().ref(`organizationData/${this.orgId}/users/${this.uId}/isDeleted/`).get().then(function(snapshot) {
      if (snapshot.exists()) {
        console.log(snapshot.val());
      }
      else {
        console.log("No data available");
      }
    }).catch(function(error) {
      console.error(error);
    });
  }

有谁知道如何修理它?

标签: angulartypescriptfirebaseionic-framework

解决方案


getUserStatus()不返回任何内容,您必须执行以下操作:

getUserStatus() : Promise<DataSnapshot> {
   return firebase.database().ref(`organizationData/${this.orgId}/users/${this.uId}/isDeleted/`).get();
  }

推荐阅读