简体   繁体   中英

#ionic change the data key in firebase

I'm trying to update/add data on firebase. I used the Facebook login and I want to use the UserID as a key for the new data aded.

(check pict below)

The userID that I want to use it:

我要使用的用户ID

I want to replace that key with the userID:

我想用用户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);

Any help?

The FB error looks correct. You cant update on the uid as the user has been saved with a unique FB id

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. However this could be an issue because the user could be saved before the return of the uid . 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.

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

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

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

.set(data) is whatever data you want to save in the users/uid namespace.

So basically you need to create that user table with its uid namespace when the user first registers. Then you can update the user with the uid returned from the facebook fs.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 ...

with lodash is it just

 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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