Firebase orderByChild equalTo在没有数据时挂起 [英] Firebase orderByChild equalTo hangs when no data
问题描述
参见jsfiddle: http://jsfiddle.net/237ur2tf/14/
coinref.orderByChild(uuid)。equalTo(xx)...
当数据库中存在匹配项时,查询可以正常工作。
当没有匹配时,不会调用回调函数和错误函数。
我做错了什么?...会怎样
非常感谢。
Pat /
这是预期的行为。
//获取子uuid并且uuid存在
coinsRef.orderByChild (uuid)
.equalTo(4D4B2118-0435-439C-BA7C-99B9BD0DA7F4)
.on('child_added',function(snapshot){
这段代码说:当有一个(现有的或新的)孩子被添加到这个查询中时,用它的快照调用我。 child_added
事件不会触发。
如果您想检查是否存在值,您应该使用值
事件:
//获取孩子uuid和uuid存在
coinsRef.orderByChild(uuid)
.equalTo(4D4B2118-0435-439C-BA7C-99B9BD0DA7F4)
.on('value' ,函数(快照){
console.log(找到钱币:4D4B2118-0435-439C-BA7C-99B9BD0DA7F4);
如果您想对特定硬币做任何事情,您需要<$ c
$ b $ $ p $ snapshot.forEach(function(child){$ c> forEach()
console.log(硬币有价值:+ child.val());
))
有什么理由不能通过他们的uuid存储硬币?这听起来像已经是一个普遍唯一的标识;所以如果可以用这个键存储它们,查找会便宜很多:
coinsRef.child(4D4B2118 ($'$'$)
.on('value',function(snapshot){
console.log(The coin has value:+ snapshot.val());
})
see 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.
If you want to check if there is a value, you should use a value
event:
// 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:
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? 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());
})
这篇关于Firebase orderByChild equalTo在没有数据时挂起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!