将用户 ID 推送到 firebase 数据库时出错

Error pushing user id to firebase DB

你好,我想在我的数据库中为每个用户存储日期,为此我想创建一个具有每个用户 UID 的节点。

我有一个使用该方法的身份验证服务:

 signupCommerce(email: string, password: string){
    return secondaryApp.auth().createUserWithEmailAndPassword(email, password).then(function(firebaseUser) {
      console.log("User " + firebaseUser.uid + " created successfully!");
       
        return firebaseUser.uid;
    });
  }

以及使用此方法的数据库服务:

createCommercePath(category:string,id:string, commerce:string, banner:string, logo: string, latitude:number, longitude:number){
    this.db.database.ref().child(category).child(id).push({
      name: commerce,
      bannerUrl: banner,
      logoUrl: logo,
      lat: latitude,
      lng: longitude
    });
  }

在我的组件中,我的表单调用此方法:

 createCommerce(){
let commerceId = this.authService.signupCommerce(this.email, this.password);
this.db.createCommercePath(this.category, commerceId, this.commerce, this.bannerUrl, this.logoUrl, this.lat,this.lng);
  }

我收到这个错误:

Argument of type 'Promise<any>' is not assignable to parameter of type 'string'.

signUpCommerce()函数returns一个Promise<any>.

let commerceId = this.authService.signupCommerce(this.email, this.password);

因此 commerceId 将是 Promise<any>

类型

您可以将 signUpCommerce 函数更改为如下内容:

signupCommerce(email: string, password: string){
    return secondaryApp.auth().createUserWithEmailAndPassword(email, password);
  }

然后在createCommerce()

中这样使用
createCommerce(){
    this.authService.signupCommerce(this.email, this.password)
    .then(firebaseUser => {
        let commerceId = firebaseUser.uid;
        this.db.createCommercePath(this.category, commerceId, this.commerce, this.bannerUrl, this.logoUrl, this.lat,this.lng);
    });
}

如错误中所述,您的方法 return 的值为 Promise<any>。据我了解,您想从此承诺中获取 return 的字符串。
因此,我建议您使用的一个选项是:(使用 rxjs,如果您的项目中还没有它,则需要 npm install 它)

import 'rxjs/add/operator/first';
import 'rxjs/Rx' ;
import 'rxjs/add/operator/toPromise';

signupCommerce(email: string, password: string){
  return secondaryApp.auth().createUserWithEmailAndPassword(email, password).first().toPromise();
}  

以上代码用于您的服务功能。 以及以下组件使用的代码:enter code here

createCommerce(){
  let commerceId = this.authService.signupCommerce(this.email, this.password).then( response => {
     this.db.createCommercePath(this.category, commerceId, this.commerce, this.bannerUrl, this.logoUrl, this.lat,this.lng);
  })
  .catch(err => console.log(err); 

}

尽情享受吧:)