[英]Firebase orderByChild equalTo hangs when no data
see jsfiddle: http://jsfiddle.net/237ur2tf/14/ 参见jsfiddle: http : //jsfiddle.net/237ur2tf/14/
coinref.orderByChild("uuid").equalTo("xx")...
Query works fine when there is a match in the database. 数据库中存在匹配项时,查询工作正常。 When there is no match, neither the callback nor error function is called.
如果没有匹配项,则不会调用回调函数或错误函数。
Am I doing something wrong?... What would be the way around this?. 我做错什么了吗?...解决方法是什么?
Many thanks. 非常感谢。 Pat/
拍/
That is the expected behavior. 那是预期的行为。 The relevant snippet from your fiddle is slightly longer:
小提琴中的相关代码段稍长:
// Get by child uuid AND uuid exists
coinsRef.orderByChild("uuid")
.equalTo("4D4B2118-0435-439C-BA7C-99B9BD0DA7F4")
.on('child_added', function(snapshot) {
This code says that "when there is an (existing or new) child added to this query, call me with its snapshot". 这段代码说:“当有一个(现有的或新的)孩子添加到该查询中时,请给我快照”。 Since there is no child, the
child_added
event doesn't fire. 由于没有孩子,因此
child_added
事件不会触发。
If you want to check if there is a value , you should use a value
event: 如果要检查是否存在值 ,则应使用
value
事件:
// Get by child uuid AND uuid exists
coinsRef.orderByChild("uuid")
.equalTo("4D4B2118-0435-439C-BA7C-99B9BD0DA7F4")
.on('value', function(snapshot) {
console.log("found Coin: 4D4B2118-0435-439C-BA7C-99B9BD0DA7F4");
If you want to do anything with the specific coin, you'll need to forEach()
in the callback: 如果您想对特定硬币进行任何操作,则需要在回调中
forEach()
:
snapshot.forEach(function(child) {
console.log("The coin has value: "+child.val());
})
Is there any reason why you can't store the coins by their uuid? 有什么理由不能用硬币的uuid来存放硬币? It sounds like that is already a universally unique identifies;
听起来这已经是一个普遍的唯一标识; so if it is possible to store them with that key, the lookup would be a lot cheaper:
因此,如果可以用该密钥存储它们,查找将便宜很多:
coinsRef.child("4D4B2118-0435-439C-BA7C-99B9BD0DA7F4")
.on('value', function(snapshot) {
console.log("The coin has value: "+snapshot.val());
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.