简体   繁体   English

Firebase orderByChild equalTo在没有数据时挂起

[英]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.

相关问题 firebase使用orderByChild()和equalTo()检索数据 - firebase retrieve data using orderByChild() and equalTo() Firebase-具有equalTo的深度查询orderByChild - Firebase - Deep Query orderByChild with equalTo Firebase .orderByChild()。equalTo()。once()。then()当子元素不存在时的Promise - Firebase .orderByChild().equalTo().once().then() Promises when child doesn't exist 在Firebase查询中组合orderByChild和equalTo - Combining orderByChild and equalTo in Firebase queries 带有 equalTo() 的 Firebase orderByChild 在 javascript 中不起作用 - Firebase orderByChild with equalTo() doesn't work in javascript 无法使用orderByChild和equalTo从Firebase查询 - Unable to query from firebase using orderByChild and equalTo Firebase 实时数据库 - orderByChild().equalTo() 不返回预期节点 - Firebase Realtime Database - orderByChild().equalTo() does not return expected node 带有path和equalTo布尔值的Firebase orderByChild不返回任何内容 - Firebase orderByChild with path and equalTo boolean doesn't return anything Firebase实时数据库:4级嵌套上的orderByChild()。equalTo() - Firebase Realtime Database: orderByChild().equalTo() on 4th level nesting 使用orderByChild和equalTo在Firebase查询上返回单个孩子的值 - Returning a single child's value on Firebase query using orderByChild and equalTo
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM