ajo*_*nno 2 javascript firebase firebase-realtime-database
以下是我要查询的Firebase参考结构:
- someData
-KgWw4iasffsD-vht3QA <=== Firebase generated key
- fieldA: '12345'
- fieldB: 'here it is'
Run Code Online (Sandbox Code Playgroud)
我想查询,关键例如.fieldB,并测试其值.例如.fieldB ='那里是'
这是我尝试但我的语法错误:
var theRef = firebase.database().ref('someData').equalTo({fieldB: 'there it was'});
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点 ?谢谢你的帮助.
是的,您可以使用orderByChild和equalTo创建查询ref:
var theRef = firebase.database()
.ref('someData')
.orderByChild('fieldB')
.equalTo('there it was');
Run Code Online (Sandbox Code Playgroud)
请注意,您需要使用Firebase安全规则创建索引.否则,someData将检索所有数据,并在客户端上执行查询.
要执行一次查询,您可以执行以下操作:
theRef.once('value',
function (snapshot) {
snapshot.forEach(function (child) {
console.log(child.key, child.val());
});
},
function (error) {
console.log(error);
}
);
Run Code Online (Sandbox Code Playgroud)
或者,使用返回的Promise:
theRef.once('value')
.then(function (snapshot) {
snapshot.forEach(function (child) {
console.log(child.key, child.val());
});
})
.catch(function (error) {
console.log(error);
});
Run Code Online (Sandbox Code Playgroud)
或者,要查询数据并继续侦听更改,您可以执行以下操作:
theRef.on('value',
function (snapshot) {
snapshot.forEach(function (child) {
console.log(child.key, child.val());
});
},
function (error) {
console.log(error);
}
);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
263 次 |
| 最近记录: |