0

I have the following JSON:

Json

I would like to add the "qtd_lote_vendido" fields, but I am not able to access them.

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); 
  }
});

The id is passing correctly.

GuR07hcFOxc1YOCqEPkL

But the field I need not

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)
Phil
  • 157,677
  • 23
  • 242
  • 245
Ribak
  • 117
  • 1
  • 3

1 Answers1

1

You want to use the value of key, not the word key:

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

becomes

console.log(snapshot.val()[key]qtd_lote_vendido); 
Graham P Heath
  • 7,009
  • 3
  • 31
  • 45