我需要使用从 Firebase 获取的 MobX 存储来更新 Material-UI 表。到目前为止,该表使用 Firebase 数据更新了“符号”列,但其他列是通用的。
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
)
}
请帮我直接获取开盘日期、开盘价格和数量。如您所见,该符号是通过 childSnapshot.key 引用的。其他三项都在推送中;我们可以使用 .next() 之类的方法访问该推送 id ...而不必知道推送 id 是什么吗?
解决方案是在 childSnapshot 上执行 forEach:
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
)
}
试试这个:
createData(childSnapshot.key, childSnapshot.dateOpened, childSnapshot.priceOpened, childSnapshot.quantity);