问题描述
这是我的Fireabse数据库结构.我想检索不是硬编码值的20170116
键的数据.它是动态键.我有一些键和值,如:
Here is my Fireabse database structure. I want to retrieve data of 20170116
keys that is not a hard coded value. It's dynamic keys. I got some keys and values like :
这是我的功能:
function getData(prospectId) {
database.ref('users/'+prospectId).once('value').then(function(snapshot) {
var prospectId = snapshot.key ;
console.log("prospectId : "+ prospectId); // output is : prospectId : 1104812
snapshot.forEach(function(childSnapshot) {
var businessUrl = childSnapshot.key;
console.log("businessUrl : "+ businessUrl); // output is : businessUrl : http:\\www.abc.com
var dates = Object.keys(childSnapshot.val());
console.log("dates : "+ dates); //output is : dates : 20170116,20170117,20170119,20170121
var singleDate = dates[0];
console.log("singleDate : "+ singleDate); //output is : singleDate : 20170116
});
});
}
getData(1104812);
那么如何获取20170116
日期数据或快照?
So how to get 20170116
date data or snapshot ?
推荐答案
您正在将值侦听器附加到/users/1104812
.因此,您在回调中获取的快照将包含位于以下子节点:20170116
,20170117
和20170119
.
You're attaching a value listener to /users/1104812
. So the snapshot you get in your callback will contain the child nodes under that: 20170116
, 20170117
and 20170119
.
当您遍历子级(使用snapshot.forEach(function(
)时,您的childSnapshot
将依次成为每个节点.
When you loop over the children (with snapshot.forEach(function(
) your childSnapshot
will become each of those nodes in turn.
这些节点都没有子节点clientUrl
或districtId
,这些子节点在树中更深一层:
None of these nodes has a child clientUrl
or districtId
, those are one level deeper into the tree:
database.ref('users/'+prospectId).once('value').then(function(snapshot) {
var prospectId = snapshot.key ;
snapshot.forEach(function(snapshot1) {
console.log(snapshot1.key); // e.g. "http://..."
snapshot.forEach(function(snapshot2) {
console.log(childSnapshot.key); // e.g. "20170116"
childSnapshot.forEach(function(snapshot3) {
console.log(grandchildSnapshot.key); // e.g. "-Kb9...gkE"
console.log(grandchildSnapshot.val().districtId); // "pne"
});
});
});
});
这篇关于如何使用javaScript在Firebase数据库中检索嵌套的子值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!