javascriptreactjsfirebase-realtime-databasemobx

How to update Material-UI table with Mobx / Firebase


I need to update a Material-UI table with a MobX store that fetches from Firebase. So far, the table updates the 'symbol' column with Firebase data, but the other columns are generic.

my table

let id = 0;
function createData(symbol, date, cost, quote, pl) {
  id += 1;
  console.log(symbol, date, cost, quote, pl)
  return { id, symbol, date, cost, quote, pl };
}

var returnArr = [];
export function getOpenTrades(user){
  db.ref('/users/' +user+ '/mocktrades/holdings/').once("value", function(snapshot) {
    snapshot.forEach(function(childSnapshot) {
      console.log('childSnapshot',childSnapshot.val()) //   <----------- shown below 
      let newData = createData(childSnapshot.key, '01012001', 0.01, 100, 100-0.01)
      returnArr.push(newData);
    })
  })
  console.log('returnArr',returnArr)
  return(
      returnArr
  )
}

enter image description here

Please help me directly access the dateOpened, priceOpened, and quantity. As you can see, the symbol is referenced with childSnapshot.key. These other three items are inside a push; can we access that push id with something like .next() ... instead of having to know what the push id is?


Solution

  • The solution was to do a forEach on the childSnapshot:

    let id = 0;
    function createData(symbol, date, cost, quote, pl) {
      id += 1;
      console.log(symbol, date, cost, quote, pl)
      return { id, symbol, date, cost, quote, pl };
    }
    
    
    
    var returnArr = [];
    export function getOpenTrades(user){
      db.ref('/users/' +user+ '/mocktrades/holdings/').once("value", function(snapshot) {
        snapshot.forEach(function(childSnapshot) {
    
          childSnapshot.forEach(function(item) {
            var itemVal = item.val()
            let newData = createData(childSnapshot.key, itemVal.dateOpened, itemVal.priceOpened, 100, 100-itemVal.priceOpened)
            returnArr.push(newData);
          })
        })
      })
      console.log('returnArr',returnArr)
      return(
          returnArr
      )
    }