2017-08-04 13 views
0

Firebaseで同じsctructureを構築する必要があります。新しいノードterriangenを作成し、{key}を追加してオブジェクトデータを設定するにはどうすればよいですか?角度とFirebase:新しいノードを作成

-usernames 
-{UID}  
    -mylibrary  
    -{key}  
    -terriangen  
    -{key}   
    type:mountain   
    name:1.png 

マイコード:

const newObjRef = firebase.database() 
       .ref('usernames') 
       .child(userId) 
       .child('mylibrary/') 
       .push(); 
      newObjRef.set({ 
       type: terrainType, 
       name: terrainName 
      }); 

答えて

0

あなたは、マルチロケーション・アップデートを探しています:

const newKey = firebase.database().ref().push.key; 
const updates = { }; 
updates["mylibrary/"+key] = { 
      type: terrainType, 
      name: terrainName 
     }; 
updates["terriangen/"+key] = { 
      type: terrainType, 
      name: terrainName 
     }; 
firebase.database() 
     .ref('usernames') 
     .child(userId) 
     .update(updates) 

最大の違い:

  • 私たちは、事前に生成しますユニークなキーで、両方の場所で使用できるようにします
  • 新しいデータのパスの一部をキーに移動し、いわゆるマルチロケーション更新を実行できます。これにより、libraryおよびterriangenノード内の既存の項目はすべて上書きされません。
+0

お返事ありがとうございます。しかし、別の場所を更新せずに新しいノード 'terriangen'を作成するだけでいいです。ですから、私が正しくあなたを理解していれば、この部分がなくてもあなたのアプローチを使うことができます - 'updates [" mylibrary/"+ key]"。そして、私の新しいノードが作成されます、正しい? – Alexander

関連する問題