我目前很难解决这个问题。您会看到,我将Firebase用作数据库来存储和获取数据。
现在,我希望能够返回在Firebase .once调用中创建的数组,但是遇到了一些困难。到目前为止,这是我的代码:
调用函数(返回函数):
<p>{this.fetchStartingPrice(singleProduct.id)}</p>
这是我要在下面显示的特定值:
fetchStartingPrice(category){
let promises = [];
promises.push(this.fetchPrices(category));
Promise.all(promises).then(result => {
console.log(result);
})
}
我刚刚使用console.log尝试对错误进行故障排除。
fetchPrices(category){
var allPrices = [];
allProductsFirebase.child(category).once('value', (snapshot) => {
snapshot.forEach((childSnapshot) => {
if(childSnapshot.key == category){
allPrices.append(childSnapshot.val().brand);
}
});
return allPrices;
})
}
因此,基本上,我想遍历allProductsFirebase尝试首先确定产品的品牌,以及是否与在fetchStartingPrice(
和fetchPrises()中用作参数的品牌匹配,我想以数字(价格)数组存储该产品的特定价格。在遍历整个快照之后,我想返回仅包含产品价格的完整数组,然后通过fetchStartingPrice()
,我想使用Math.min(promises)
来获取该数组中的最低编号。但是,我很难做到这一点。有人可以帮我吗?
之后,我希望能够在fetchStartingPrice()
中返回值。
答案 0 :(得分:0)
fetchPrices()
必须返回Promise
或为Promise
。您没有从fetchPrices()
返回任何内容(在allPrices
范围内返回了.once()
)。尝试返回Promise
返回的结果(如果返回.once()
)。
fetchPrices(category){
var allPrices = [];
return allProductsFirebase.child(category).once('value', (snapshot) => {
snapshot.forEach((childSnapshot) => {
if(childSnapshot.key == category){
allPrices.append(childSnapshot.val().brand);
}
});
return allPrices;
})
}