实时数据交互Firebase

时间:2018-10-11 22:55:33

标签: javascript firebase firebase-realtime-database

我有以下JSON:

Json

我想添加“ qtd_lote_vendido”字段,但是我无法访问它们。

var query = dbrealtime.ref('eventos/Bzu0eH4jTdbIQmcRuCSa/lotes');
query.on('value', function(snapshot) {
  for (var key in snapshot.val()) {
    console.log(key);
    console.log(snapshot.val().key.qtd_lote_vendido); 
  }
});

ID正确传递。

GuR07hcFOxc1YOCqEPkL

但是我不需要的领域

firebase.js:1 Uncaught TypeError: Cannot read property 'qtd_lote_vendido' of undefined
    at (index):46
    at firebase.js:1
    at exceptionGuard (firebase.js:1)
    at e.raise (firebase.js:1)
    at e.raiseQueuedEventsMatchingPredicate_ (firebase.js:1)
    at e.raiseEventsForChangedPath (firebase.js:1)
    at e.onDataUpdate_ (firebase.js:1)
    at t.onDataPush_ (firebase.js:1)
    at t.onDataMessage_ (firebase.js:1)
    at e.onDataMessage_ (firebase.js:1)

1 个答案:

答案 0 :(得分:1)

您要使用key的值,而不是单词key:

console.log(snapshot.val().key.qtd_lote_vendido); 

成为

console.log(snapshot.val()[key]qtd_lote_vendido);