简体   繁体   English

#ionic更改Firebase中的数据密钥

[英]#ionic change the data key in firebase

I'm trying to update/add data on firebase. 我正在尝试更新/添加Firebase上的数据。 I used the Facebook login and I want to use the UserID as a key for the new data aded. 我使用了Facebook登录名,并且想将UserID用作添加新数据的密钥。

(check pict below) (请查看下面的图片)

The userID that I want to use it: 我要使用的用户ID:

我要使用的用户ID

I want to replace that key with the 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);
  });

I got this error in compilation: 我在编译时遇到此错误:

supplied parameters do not matchany signature of call target 提供的参数与呼叫目标的任何签名都不匹配

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

Any help? 有什么帮助吗?

The FB error looks correct. FB错误看起来正确。 You cant update on the uid as the user has been saved with a unique FB id 由于用户已使用唯一的FB id保存,因此无法更新uid

You do not show the code that created the users record in the database, but what i think you want to do is set and object when you first save the users record. 您没有显示在数据库中创建users记录的代码,但是我认为您想要做的是在您第一次保存users记录时设置和对象。 However this could be an issue because the user could be saved before the return of the uid . 但是,这可能是一个问题,因为可以在返回uid之前保存user I cant tell with your code snippet. 我无法告诉您的代码段。 Regardless, I will write the code that i think will work if the users/ record is created at the time that of registration. 无论如何,如果users/记录是在注册时创建的,我将编写我认为会起作用的代码。

The service 服务

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

So this initially creates a user without facebook, The key here is that the users FB uid was created and is held in the returned result 因此,这最初会创建一个没有facebook的用户,这里的关键是创建了用户FB uid并将其保存在返回的result

The component 组件

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

Then we create a record in FB with the uid returned 然后我们在FB中创建一个记录,并返回uid

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

.set(data) is whatever data you want to save in the users/uid namespace. .set(data)是要保存在users/uid名称空间中的任何数据。

So basically you need to create that user table with its uid namespace when the user first registers. 因此,基本上,您需要在用户首次注册时使用其uid名称空间创建该用户表。 Then you can update the user with the uid returned from the facebook fs.uid 然后,您可以使用从Facebook fs.uid返回的uid更新用户。


With your current code you could find the user based on the email ( because the email should be unique to all users) and then update ... 使用您当前的代码,您可以根据电子邮件find用户(因为电子邮件对于所有用户而言都是唯一的),然后进行更新...

with lodash is it just 与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);

i fixed the problem by using: 我通过使用以下方式解决了问题:

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

instead of : 代替 :

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

And it works correctly ! 而且它正常工作! Thanks all for help. 谢谢大家的帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM