gpt4 book ai didi

javascript - 从 firebase 检索数据导致未定义

转载 作者:行者123 更新时间:2023-12-01 03:02:16 24 4
gpt4 key购买 nike

我在尝试从 firebase 中拉出时遇到了这个问题。我的数据库结构如下:

我正在尝试提取merchantName及其相关分支机构的详细信息:

function getAllMerchant(){
var query = firebase.database().ref('merchants');
return query.once('value').then(data => {
var result = [];
data.forEach(snapshot => {
var merchantData = snapshot.val();
var merchantName = merchantData.merchantName;
var branchName = merchantData.branches.branchName;
var branchAddress = merchantData.branches.branchAddress;
console.log('check ' + merchantName + ' ' + branchName + ' ' + branchAddress);
result.push({merchantName: merchantName, branchName: branchName, branchAddress: branchAddress});
});
return result;
});
resolve(result);
}

但是,当我打印出branchName和branchAddress时,我得到了未定义的信息。不过我设法打印出了merchantName。有什么想法吗?

谢谢!

最佳答案

您没有迭代商家的分支

data.forEach(merchantSnapshot => {
var merchantData = snapshot.val();
var merchantName = merchantData.merchantName;
console.log('check ' + merchantName);
merchantSnapshot.child("branches").forEach(brancheSnapshot => {
var branchName = brancheSnapshot.val().branchName;
var branchAddress = brancheSnapshot.val.branchAddress;
console.log(' ' + branchName + ' ' + branchAddress);
});
});

关于javascript - 从 firebase 检索数据导致未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46380209/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com