2016-04-09 82 views
0

我是push將對象放入我的ROOMS數組中,其中.on("child_added", ....)如下所示。該對象的其中一個部分是其他對象的數組(Playerobj,它具有一些屬性,如id,name等)。我如何分配給ROOMS.players因爲snap.child("players").val()不起作用(EXCEPTION: Error trying to diff '[object Object]'snap.child("players").val()結構看起來是這樣的:將FirebaseDataSnapshot轉換爲JS對象

-rooms 
    -id 
    -name 
    -isRoomFull 
    +board 
    -players 
     -kEFvdfeff84fdfdff 
      -id 
      -name 
     -kEFvd4545dfjh9fvv 
      -id 
      -name 



getRooms() { 
this.roomsRef.on("child_added", function(snap) { 
    ROOMS.push({ 
     id: snap.child("id").val(), 
     name: snap.child("name").val(), 
     players: [], //how to assign to this property 
     isRoomFull: snap.child("isRoomFull").val(), 
     board: snap.child("board").val(), 
    }) 
}); 

回答

0

我會嘗試這樣的事情:

var obj = snap.child("players").val().players; 
var players = Object.keys(obj).map(key => return obj[key]); 

ROOMS.push({ 
    id: snap.child("id").val(), 
    name: snap.child("name").val(), 
    players: players, 
    isRoomFull: snap.child("isRoomFull").val(), 
    board: snap.child("board").val(), 
}); 

詳情請參閱此問題:

+0

這很好。我不得不修改一些東西 'var obj = snap.child(「players」)。val(); var player = Object.keys(obj).map(key => {return obj [key]});' –