代码之家  ›  专栏  ›  技术社区  ›  AlbertMunichMar

JS Promise内部方法

  •  0
  • AlbertMunichMar  · 技术社区  · 6 年前

    我有一个通过FireBase向用户签名的方法。我有一个承诺,当它完成,然后,我得到了令牌。

    signinUser(email: string, password: string) {
        return firebase.auth().signInWithEmailAndPassword(email, password)
            .then(
                response => {
                    this.router.navigate(['/']);
                    firebase.auth().currentUser.getIdToken()
                        .then(
                            (token: string) => {
                                this.token = token
                            }
                        )
                }
            )
            .catch(
                error => console.log(error)
            );
    }
    

    现在,我从另一个文件调用这个方法,我想在signinuser方法完成它的承诺后做些什么。

      onSignin(form: NgForm) {
        const email = form.value.email;
        const password = form.value.password;
        this.authService.signinUser(email, password)
          .then(
            res => console.log(res)
          )
    
      }
    

    我得到的结果是未定义的。为什么?

    1 回复  |  直到 6 年前
        1
  •  1
  •   Rahul Sharma    6 年前

    你没有从承诺中返还代币

    signinUser(email: string, password: string) {
        return firebase.auth().signInWithEmailAndPassword(email, password)
            .then((response) => {
                this.router.navigate(['/']);
                return firebase.auth().currentUser.getIdToken()
                    .then((token: string) => {
                        this.token = token
                        return token;
                    })
            })
            .catch(
                error => console.log(error)
            );
    }