【问题标题】:#ionic change the data key in firebase#ionic 更改 firebase 中的数据键
【发布时间】:2017-07-14 19:09:03
【问题描述】:

我正在尝试在 firebase 上更新/添加数据。我使用了 Facebook 登录,我想使用 UserID 作为新数据的键。

(查看下图)

我想使用的用户ID:

我想用用户 ID 替换那个键:

 fblogin(){
this.facebook.login(['email'])
.then(res=> {
const fc = firebase.auth.FacebookAuthProvider.credential(res.authResponse.accessToken);
firebase.auth().signInWithCredential(fc) 
    .then(fs => {

    this.facebook.api('me?fields=id,name,email,first_name,picture.width(720).height(720).as(picture_large)', []).then(profile => {
    this.newuser = {name: profile['first_name'] ,email: profile['email'],picture: profile['picture_large']['data']['url'],phone:''}
    this.navCtrl.push(HomePage);
    console.log(fs.uid);
    this.db.list('/users/'+ fs.uid).update(this.newuser);
  });

我在编译时遇到了这个错误:

提供的参数与调用目标的任何签名都不匹配

在这一行:this.db.list('/users/'+ fs.uid).update(this.newuser);

有什么帮助吗?

【问题讨论】:

    标签: javascript firebase ionic-framework firebase-realtime-database ionic3


    【解决方案1】:

    FB 错误看起来是正确的。您无法在 uid 上更新,因为用户已使用唯一的 FB id 保存

    您没有显示在数据库中创建 users 记录的代码,但我认为您想要做的是在您第一次保存 users 记录时设置和对象。然而这可能是一个问题,因为user 可以在uid 返回之前保存。我不能用你的代码sn-p来判断。无论如何,如果在注册时创建了users/ 记录,我将编写我认为可以工作的代码。

    服务

    async signupUser(email: string, password: string) {
      try {
        const result = await this.afA.auth.createUserWithEmailAndPassword(email, password);
        return result;
      } catch (err) {
        console.log('error', err);
      }
    }
    

    所以这最初创建了一个没有facebook的用户,这里的关键是用户FBuid被创建并保存在返回的result

    组件

    this.authData.signupUser(email,password).then(userData => {
       console.log(userData) // <- this is result 
    }
    

    然后我们在 FB 中创建一条记录,返回 uid

    this.db.object(`users/${userData.uid}/`).set(data); 
    

    .set(data) 是您要保存在users/uid 命名空间中的任何数据。

    所以基本上你需要在用户第一次注册时创建带有 uid 命名空间的用户表。然后您可以使用从 facebook 返回的 uid 更新用户 fs.uid


    使用您当前的代码,您可以根据电子邮件find 用户(因为电子邮件应该对所有用户都是唯一的)然后更新...

    用lodash只是

     let foundUser = find(this.db.list('users'),{ 'email' : fs.email }
    
     // and then update based on the object key
    
     this.db.list('/users/'+ Object.keys(foundUser)).update(this.newuser);
    

    【讨论】:

      【解决方案2】:

      我通过使用解决了这个问题:

      this.db.object('/users/'+ fs.uid).update(this.newuser); 
      

      而不是:

      this.db.list('/users/'+ fs.uid).update(this.newuser);
      

      而且它工作正常! 谢谢大家的帮助。

      【讨论】:

        猜你喜欢
        • 2019-04-08
        • 1970-01-01
        • 1970-01-01
        • 2019-03-27
        • 1970-01-01
        • 2020-05-05
        • 2021-12-06
        • 1970-01-01
        • 2018-11-19
        相关资源
        最近更新 更多