2017-10-10 22 views
1

我有以下內容,只需將一個名稱與uid一起推入數據庫。我想在同一時間將該uid存儲在另一個位置。我怎樣才能做到這一點?我使用離子3,angularfire2。將firebase uid推到多個位置 - Angularfire2 Ionic 3

addName() { 
     let prompt = this.alertCtrl.create({ 
     title: 'Name of user', 
     message: "Enter a name for this new user", 
     inputs: [ 
      { 
      name: 'Name', 
      placeholder: 'Username' 
      }, 
     ], 
     buttons: [ 
      { 
      text: 'Cancel', 
      handler: data => { 
       console.log('Cancel clicked'); 
      } 
      }, 
      { 
      text: 'Save', 
      handler: data => { 
       this.users.push({ 
       title: data.Name, 

       }); 

       this.posts.push({ 
       title: data.Name 
       }); 
      } 
      } 
     ] 
     }); 
     prompt.present(); 
    } 

在此先感謝

回答

3

您的代碼不會驗證用戶,所以沒有參與UID(以下簡稱用戶ID)。

但是,如果你正在尋找使用相同的推ID寫入兩個位置時:

handler: data => { 
    var key = this.users.push().key; 
    this.users.child(key).set({ 
    title: data.Name, 

    }); 

    this.posts.child(key).set({ 
    title: data.Name 
    }); 
} 

你甚至可以將二者結合起來set操作成一個單一的更新,但我不知道你的usersposts引用是如何相關的。如果兩者都是根源的直接孩子,那應該是:

handler: data => { 
    var key = this.users.push().key; 
    var updates = {}; 
    updates["users/"+key+"/title"] = data.Name; 
    updates["posts/"+key+"/title"] = data.Name; 
    this.ref.update(updates); 
} 
+0

謝謝弗蘭克,正是我所需要的。對不起,我所指的是推送ID或唯一ID。再次感謝! – MacD

相關問題