我正在尝试从Firebase数据库中获取数据。但是,每次获取数据时,对于数据库中的子计数次数,我都会得到相同的值。
在这种情况下,我想获取键值并将其推入数组
data.forEach((vals) => {
keys.push(vals.key)
})
但是在每个索引的键数组中,我获得了子级的整个键值,而不是一个单独的索引中的每个键值,如何解决此问题。
预期输出:
[
"-LHvNl1cADFEwkyzRb7n",
"-LHw6EX2Nf8vGhIUwLr4",
"-LHwnhCA86JX6H3iDtUZ"
]
我现在得到的是:(
[
"-LHvNl1cADFEwkyzRb7n",
"-LHw6EX2Nf8vGhIUwLr4",
"-LHwnhCA86JX6H3iDtUZ"
]
[
"-LHvNl1cADFEwkyzRb7n",
"-LHw6EX2Nf8vGhIUwLr4",
"-LHwnhCA86JX6H3iDtUZ"
]
[
"-LHvNl1cADFEwkyzRb7n",
"-LHw6EX2Nf8vGhIUwLr4",
"-LHwnhCA86JX6H3iDtUZ"
]
[
"-LHvNl1cADFEwkyzRb7n",
"-LHw6EX2Nf8vGhIUwLr4",
"-LHwnhCA86JX6H3iDtUZ"
]
[
"-LHvNl1cADFEwkyzRb7n",
"-LHw6EX2Nf8vGhIUwLr4",
"-LHwnhCA86JX6H3iDtUZ"
]
[
"-LHvNl1cADFEwkyzRb7n",
"-LHw6EX2Nf8vGhIUwLr4",
"-LHwnhCA86JX6H3iDtUZ"
]
JSON数据
"21-jul-2018" {
"-LHvNl1cADFEwkyzRb7n" : {
"hotel" : "B2C",
"items" : {
"Dosai" : {
"count" : 2,
"header" : "Dosai",
"image" : "https://firebasestorage.googleapis.com/v0/b/annam-6e679.appspot.com/o/Deals%2FDosai?alt=media&token=3d105c54-e2c2-4a78-9de5-792b5c82a170",
"key" : "B2C",
"name" : "B2C",
"offerPrice" : "2.00",
"price" : "2.38"
},
"shawaramas" : {
"count" : 1,
"header" : "shawaramas",
"image" : "https://firebasestorage.googleapis.com/v0/b/annam-6e679.appspot.com/o/Specials%2Fshawaramas?alt=media&token=eb1ec339-9803-4c75-888e-908f9fe1abb6",
"key" : "B2C",
"name" : "B2C",
"price" : "4.48"
}
},
"key" : "-LHvNl1cADFEwkyzRb7n",
"status" : 0,
"time" : "11:19:47",
"user" : "ashwin20@gmail.com"
},
"-LHw6EX2Nf8vGhIUwLr4" : {
"hotel" : "B2C",
"items" : {
"Idly" : {
"count" : 2,
"header" : "Idly",
"image" : "https://firebasestorage.googleapis.com/v0/b/annam-6e679.appspot.com/o/Deals%2FIdly?alt=media&token=1022e77d-d114-448b-8f71-c3b67e578ad6",
"key" : "B2C",
"name" : "B2C",
"offerPrice" : "1.36",
"price" : "2.36"
},
"shawaramas" : {
"count" : 1,
"header" : "shawaramas",
"image" : "https://firebasestorage.googleapis.com/v0/b/annam-6e679.appspot.com/o/Specials%2Fshawaramas?alt=media&token=eb1ec339-9803-4c75-888e-908f9fe1abb6",
"key" : "B2C",
"name" : "B2C",
"price" : "4.48"
}
},
"key" : "-LHw6EX2Nf8vGhIUwLr4",
"status" : 0,
"time" : "09:12:52",
"user" : "ashwin20@gmail.com"
},
"-LHwnhCA86JX6H3iDtUZ" : {
"hotel" : "B2C",
"items" : {
"shawaramas" : {
"count" : 1,
"header" : "shawaramas",
"image" : "https://firebasestorage.googleapis.com/v0/b/annam-6e679.appspot.com/o/Specials%2Fshawaramas?alt=media&token=eb1ec339-9803-4c75-888e-908f9fe1abb6",
"key" : "B2C",
"name" : "B2C",
"price" : "4.48"
}
},
"key" : "-LHwnhCA86JX6H3iDtUZ",
"status" : 0,
"time" : "12:27:08",
"user" : "ashwin20@gmail.com"
}
}
我的代码
this.path = firebase.database().ref('orders').child(dateformat(new Date(), 'dd-mmm-yyyy').toLowerCase())
this.path.on('value', (data) => {
var list = data.val();
var items = [];
for(var key in list) {
var item = list[key];
item.key = key;
items[items.length] = item;
}
// var key = Object.keys(data.val());
items.forEach((data) => {
keysVal.push(data.key)
})
keysVal.forEach((key) => {
this.newPath = firebase.database().ref('orders').child(dateformat(new Date(), 'dd-mmm-yyyy').toLowerCase()).child(key).child('items')
this.newPath.on('value', snap => {
snap.forEach((names) => {
vals.push(names.key)
this.newPath.child(names.key).on('value', data => {
itemsNames.push(data.val())
// console.log(data.val())
})
})
})
})