简体   繁体   中英

Save with firebase with a dynamic key name

I am trying to insert in my database the month chosen by the user.

saveInBdd (){
  this.errors = [];

  const user = firebase.auth().currentUser;
  user.updateProfile({
    displayName: this.firstname,
  }).then(()=>{
    this.saveUserToUsersRef(user)
  }, error => {
    this.errors.push(error.message)
  })
},
saveUserToUsersRef(user){
  return this.usersRef.child(user.uid).update({
    tab1: { this.months[0].selectedOption : "test"}
  })
},

This code returns this error to me:

在此处输入图片说明

The property name of a JSON object in the notation you're using needs to be a literal. To use a variable as the name of a property, use [] notation:

saveUserToUsersRef(user){
  var updates = {};
  updates[this.months[0].selectedOption] = "test";
  return this.usersRef.child(user.uid).update(updates))
},

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