2017-06-13 73 views
0

您好,我希望为每个用户在我的数据库中存储日期,因为我希望为每个用户创建一个包含UID的节点。将用户ID推送到firebase数据库时出错DB

我对此方法的认证服务:

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

而且这种方法的DB服务:

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'.

回答

0

signUpCommerce() - 函数返回一个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); 
    }); 
} 
+0

谢谢您的回答,我怎样才能得到验证服务的uid和推到DB。 –

+0

孩子的方法需要一个字符串我不能通过一个Promise –

+0

谢谢,那工程,但我不知道有什么区别。 –

0

A在错误中描述的,你的方法返回值为Promise<any>。据我所知,你想获得从这个承诺返回的字符串。
所以,一个选项,我建议你使用的是:(使用rxjs,你需要NPM,如果你没有在您的项目尚未安装)

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); 

} 

享受:)

相关问题