簡體   English   中英

使用動態鍵名使用Firebase保存

[英]Save with firebase with a dynamic key name

我試圖在數據庫中插入用戶選擇的月份。

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"}
  })
},

此代碼將此錯誤返回給我:

在此處輸入圖片說明

您正在使用的符號中的JSON對象的屬性名稱必須為文字。 要將變量用作屬性名稱,請使用[]表示法:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM