I don't know which language (which Client SDK) you use in your app, but here is a solution with the JavaScript SDK.
(我不知道您在应用中使用哪种语言(哪种客户端SDK),但这是JavaScript SDK的解决方案。)
Let's imagine you have a Relatime Database structure as follows
(假设您有一个Relatime数据库结构,如下所示)
"parentnode" : {
"currencies" : {
"EUR" : {
"value" : {
"price" : 201
}
},
"USD" : {
"value" : {
"price" : 343
}
},
"value" : {
"EUR_price" : 201,
"USD_price" : 343,
"currency-source" : "fx"
}
}
}
Under a parentnode
you have a currencies
node which corresponds to the examples in your question .
(在parentnode
节点下,您有一个currencies
节点,该节点与问题中的示例相对应 。)
In case you want to listen to /currencies/<currency>/value
, you would do as follows:
(如果您想收听/currencies/<currency>/value
,请执行以下操作:)
var db = firebase.database();
var currency = 'EUR';
var ref = db.ref().child('parentnode/currencies/' + currency);
ref.on('value', function(snapshot) {
var data = snapshot.val();
console.log(data);
});
In case you want to listen to /currencies/value/<currency>_price
and get the price
and the currency-source
values, you would do as follows:
(如果您想收听/currencies/value/<currency>_price
并获取price
和currency-source
值,请执行以下操作:)
var db = firebase.database();
var currency = 'EUR';
var ref = db.ref().child('parentnode/currencies/value');
ref.on('value', function(snapshot) {
var data = snapshot.val();
var price = data[currency + '_price'];
var source = data['currency-source'];
console.log(price);
console.log(source);
});
As mentioned in your comment, the second approach implies that "we will download all the data leaf under /currencies/value/
".
(如您的评论中所述,第二种方法意味着“我们将在/currencies/value/
下下载所有数据叶”。)
I can think of two other possible approaches.
(我可以想到另外两种可能的方法。)
Choosing one over the other really depends on your functional requirements, ie what you do with these values in your front-end. (一个选择另一个实际上取决于您的功能要求,即您在前端中如何使用这些值。)
1/ Set two listeners (1 /设置两个听众)
The idea is to set one listener for 'parentnode/currencies/value/' + currency + '_price'
and one for 'parentnode/currencies/value/currency-source'
, as follows:
(想法是为'parentnode/currencies/value/' + currency + '_price'
设置一个侦听器,为'parentnode/currencies/value/currency-source'
设置一个侦听器,如下所示:)
var currency = 'EUR';
var ref2 = db
.ref()
.child('parentnode/currencies/value/' + currency + '_price');
ref2.on('value', function(snapshot) {
var data = snapshot.val();
console.log(data);
});
var ref3 = db.ref().child('parentnode/currencies/value/currency-source');
ref3.on('value', function(snapshot) {
var data = snapshot.val();
console.log(data);
});
2/ Query the currency-source
value within the listener (2 /在侦听器中查询currency-source
值)
With this second approach, in the listener to 'parentnode/currencies/value/' + currency + '_price'
, we query the database with the once()
method to get the value of currency-source
:
(通过第二种方法,在侦听'parentnode/currencies/value/' + currency + '_price'
,我们使用'parentnode/currencies/value/' + currency + '_price'
once()
方法查询数据库以获取currency-source
的值:)
var ref4 = db
.ref()
.child('parentnode/currencies/value/' + currency + '_price');
ref4.on('value', function(snapshot) {
var data = snapshot.val();
console.log(data);
db.ref()
.child('parentnode/currencies/value/currency-source')
.once('value')
.then(function(dataSnapshot) {
console.log(dataSnapshot.val());
});
});
Note that if do not need to set a listener, ie you want to fetch the data once (eg by triggering the fetch from a button, or on page loading, etc..) you should only use the once()
method and then you can chain the two calls as follows:
(请注意,如果不需要设置侦听器,即您想一次获取数据 (例如,通过触发按钮获取或在页面加载等方式进行获取),则只应使用once()
方法,然后可以如下链接两个调用:)
var currency = 'EUR';
var ref5 = db.ref().child('parentnode/currencies/value/' + currency + '_price');
var ref6 = db.ref().child('parentnode/currencies/value/currency-source');
ref5
.once('value')
.then(function(dataSnapshot) {
console.log(dataSnapshot.val());
return ref6.once('value');
})
.then(function(dataSnapshot) {
console.log(dataSnapshot.val());
});