从firebase检索数据导致未定义

时间:2017-09-23 13:41:05

标签: javascript firebase firebase-realtime-database

我试图从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。有什么想法吗?

谢谢!

1 个答案:

答案 0 :(得分:2)

您没有迭代商家的data = "012021111" function isGameOver(data){ var map = []; var index = 0; for (var i = 0; i < 3; i++) { map.push([]); for (var j = 0; j < 3; j++) { map[i][j] = data[index]; console.log("index: " +index+ ",i: " + i + ",j: " + j + ", data: " + map[i][j]); index++; } } } isGameOver(data);

branches