【问题标题】:Firebase orderByChild equalTo hangs when no dataFirebase orderByChild equalTo 在没有数据时挂起
【发布时间】:2016-03-08 19:40:58
【问题描述】:

见 jsfiddle:http://jsfiddle.net/237ur2tf/14/

coinref.orderByChild("uuid").equalTo("xx")...

当数据库中有匹配项时,查询工作正常。 当不匹配时,既不调用回调函数,也不调用错误函数。

我是不是做错了什么?...有什么办法可以解决这个问题?

非常感谢。 帕特/

【问题讨论】:

    标签: javascript firebase


    【解决方案1】:

    这是预期的行为。您小提琴中的相关 sn-p 稍长:

    // Get by child uuid AND uuid exists
    coinsRef.orderByChild("uuid")
            .equalTo("4D4B2118-0435-439C-BA7C-99B9BD0DA7F4")
            .on('child_added', function(snapshot) {
    

    这段代码说“当有一个(现有的或新的)孩子添加到这个查询中时,用它的快照给我打电话”。由于没有孩子,child_added 事件不会触发。

    如果你想检查是否有值,你应该使用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");
    

    如果你想对特定硬币做任何事情,你需要在回调中forEach()

    snapshot.forEach(function(child) {
        console.log("The coin has value: "+child.val());
    })
    

    你有什么理由不能通过它们的 uuid 存储硬币吗?听起来这已经是一个普遍唯一的标识;因此,如果可以使用该密钥存储它们,那么查找会便宜很多:

    coinsRef.child("4D4B2118-0435-439C-BA7C-99B9BD0DA7F4")
            .on('value', function(snapshot) {
        console.log("The coin has value: "+snapshot.val());
    })
    

    【讨论】:

    • 对!解决方案是使用“值”来检测无效的 id。可以检查快照返回值。 “child_added”不会触发,因为没有价值。有理由不使用 uuid 作为密钥……但这不是重点 :) 非常感谢您的帮助。
    • @Fran van Puffelen,java 的等价物是什么?
    猜你喜欢
    • 2017-08-22
    • 1970-01-01
    • 2019-09-13
    • 2018-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多