1 回答

TA貢獻1777條經(jīng)驗 獲得超10個贊
假設你想從 firestore 獲取數(shù)據(jù)集合,你應該首先獲取該集合的引用:
const currencyRef = firestore().collection('CURRENCY-PAIR');
const alertRef = firestore().collection('Alert_Status');
然后,您可以使用這些引用從 firestore 獲取數(shù)據(jù):
currencyRef.get()
.then((doc) => {
console.log(doc.data());
});
如您所見,數(shù)據(jù)采用承諾的形式,您必須解決該承諾。doc.data() 是集合中所有數(shù)據(jù)的數(shù)組,采用 JS 對象的形式。
由于數(shù)據(jù)作為承諾而來,因此您可以創(chuàng)建一個異步獲取函數(shù)來解析承諾,并將數(shù)據(jù)放入返回的新數(shù)組中。也許你可以這樣做:
const fetchAllCurrencies = async () => {
const obj = []; // empty array to put collections in
const currencyRef = firestore().collection('CURRENCY-PAIR'); // ref
const snapshot = await currencyRef.get() // resolve promise from firestore
snapshot.forEach((doc) => { // loop over data
obj.push({ id: doc.id, ...doc.data() }); // push each collection to array
});
return obj; // return array with collection objects
}
您可以為警報集合創(chuàng)建類似的函數(shù)。
我不完全確定您的意思:“我如何將 CURRENCY-PAIR 集合中的貨幣對名稱和 Alerts 集合中的 Alert_Status 映射到顯示兩者的列表?!?/p>
通過創(chuàng)建像上面這樣的函數(shù),您可以獲得集合 js 對象的數(shù)組。您可以將兩個數(shù)組組合起來:
const newArray = array1.concat(array2);
這會將兩個數(shù)組融合為一個。這可能不是您想要做的。如果我是你,我會將兩個數(shù)組分開。
添加回答
舉報