【问题标题】:How to get firebase snapshot of matching children?如何获取匹配孩子的firebase快照?
【发布时间】:2017-11-01 21:41:21
【问题描述】:

我有一个这种格式的 firebase 数据结构

当用户在搜索字段中输入 5133-01-329-5971 时,我希望能够获得快照

5133-01-329-5971-OkqcPp3xJwfgmNinwGsKZmAa8xt1-1508951718305

5133-01-329-5971-OkqcPp3xJwfgmNinwGsKZmAa8xt1-1508966816945

因为它们包含用户输入的匹配项。

这段代码将给我节点中的所有孩子

  var itemdetailref = Cataloguedatabase.ref('/Listings/');

  return itemdetailref.once('value').then(function(snapshot){

  })

如何获取与 5133-01-329-5971 匹配的所有孩子的快照?

【问题讨论】:

    标签: javascript firebase search firebase-realtime-database


    【解决方案1】:

    如果您只需要一个特殊的孩子,您可以在 ref var 中输入此字符串。像这样的:

    let uid = "5133-01-329-5971";
    let child = "-OkqcPp3xJwfgmNinwGsKZmAa8xt1-1508966816945";
    var itemdetailref = Cataloguedatabase.ref('/Listings/'+uid+child);
    
    return itemdetailref.once('value').then(function(snapshot){
    
    })
    

    但是,如果您像在示例中那样搜索多个孩子,则必须获取每个孩子,然后只获取包含您的用户 ID 的这些孩子。比如:

    var itemdetailref = Cataloguedatabase.ref('/Listings/'+uid+child);
    
    return itemdetailref.once('value').then(function(snapshot){
            snapshot.forEach(childSnapshot => {
                let key = childSnapshot.key;
                let uid = "5133-01-329-5971";
                //use ES6 includes function
                if(key.includes(uid)){
                    myFunction(childSnapshot.val());
                }
            });
    })
    

    【讨论】:

      猜你喜欢
      • 2017-02-04
      • 1970-01-01
      • 2014-12-15
      • 1970-01-01
      • 2019-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-28
      相关资源
      最近更新 更多