如何在Firebase中浏览快照

时间:2019-09-22 13:04:46

标签: javascript firebase vue.js firebase-realtime-database

我的firebase结构如下:

enter image description here

以下代码在选择框(2019/9/7 AMS MK7777)中搜索用户选择的文本,并返回ulds对象

FBLoad: function(){
            console.log($(".text").text())              

            var ref = firebase.database().ref("flights");
            ref
            .orderByChild("queryDes")
            .equalTo($(".text").text())
            .once("child_added", function(snapshot) {
                console.log(snapshot.val().ulds);
            });
}

我要完成的是对象的数据由ulds对象的数据更新 需要更新的对象如下:

var positions =  {
    "A": {h_arm:14645, "occupied":0, "ULD":"", "DEST":"", "CNTR":"", "WGHT":0, "TYPE":"", "SHC":[], "RMRK":""},
    "BR": {h_arm:16905,  "occupied":0, "ULD":"", "DEST":"", "CNTR":"", "WGHT":0, "TYPE":"", "SHC":[], "RMRK":""},
    "C": {h_arm:19165, "occupied":0, "ULD":"", "DEST":"", "CNTR":"", "WGHT":0, "TYPE":"", "SHC":[], "RMRK":""},
    "R": {h_arm:19165, "occupied":0, "ULD":"", "DEST":"", "CNTR":"", "WGHT":0, "TYPE":"", "SHC":[], "RMRK":""}
}

最终结果应该是这样的:

var positions =  {
    "A": {h_arm:14645, "occupied":0, "ULD":"", "DEST":"", "CNTR":"", "WGHT":0, "TYPE":"", "SHC":[], "RMRK":""},
    "BR": {h_arm:16905,  "occupied":0, "ULD":"PMC12345QR", "DEST":"DOH", "CNTR":"Q5", "WGHT":2500, "TYPE":"C", "SHC":["RFL","RCM"], "RMRK":"OHG75CMF"},
    "C": {h_arm:19165, "occupied":0, "ULD":"", "DEST":"", "CNTR":"", "WGHT":0, "TYPE":"", "SHC":[], "RMRK":""},
    "R": {h_arm:19165, "occupied":0, "ULD":"PMC77785QR", "DEST":"DOH", "CNTR":"Q5", "WGHT":3540, "TYPE":"C", "SHC":["PIL"], "RMRK":"IND75CMA"}
}

我找不到遍历ulds-对象的方法

1 个答案:

答案 0 :(得分:2)

要遍历API 23,请尝试以下操作:

ulds

首先,使用事件once("value", function(snapshot) { snapshot.forEach(function(childSnapshot){ childSnapshot.forEach(function(uldSnapshot){ uldSnapshot.forEach(function(nestedUldSnapshot){ console.log(nestedUldSnapshot.val()); }); }); 检索数据,然后使用value检索随机ID下的数据。然后使用另一个forEach()访问forEach()对象内部的数据。