简体   繁体   English

Firebase orderByKey()。startAt()无法按预期工作。 怎么了?

[英]Firebase orderByKey().startAt() not working as expected. What's wrong?

I'm trying to get firebase data from a node which uid must start with a passed string. 我正在尝试从一个节点获取firebase数据,该节点必须以传递的字符串开头。

I tried a code but I always get the same data. 我尝试了一个代码,但我总是得到相同的数据。 The database data is as following: 数据库数据如下:

在此输入图像描述

And I'm using the following code: 我正在使用以下代码:

var ref = firebase.database().ref("restaurantes/history");
    ref.orderByKey().startAt(userUID).once("child_added", function(snapshot) {
      snapshot.forEach(child => {
        if(child.key == "orders")
        {
          console.log(child.val());

          _.each(child.val(), (value, key) => {
            arrtmp.push(value)
          })
        }    

      })

If user is "FKQLlqa" I should get the history data shown in the picture. 如果用户是“FKQLlqa”,我应该得到图片中显示的历史数据。 If I user is "abc" I shouldn't get any data. 如果我的用户是“abc”,我不应该得到任何数据。 But I always get the data shown in the picture. 但我总是得到图中显示的数据。 Should I use another way of querying? 我应该使用其他查询方式吗? Or I should use a key field inside orders and payments data? 或者我应该在订单和付款数据中使用关键字段?

Regards! 问候!

Try the following: 请尝试以下方法:

var ref = firebase.database().ref("restaurantes/history");
ref.child(userUID).once("value", function(snapshot) {
  if (snapshot.exists()) {
      console.log(snapshot.val());
    }
  else {
      console.log("different user");    
  });

This will check if the snapshot that contains the userId (added as a parameter in the child() method), already exists in the database then you will be able to retrieve the data under the userId . 这将检查包含userId的快照(在child()方法中添加为参数)是否已存在于数据库中,然后您将能够在userId下检索数据。

For reference: 以供参考:

https://firebase.google.com/docs/reference/js/firebase.database.DataSnapshot#exists https://firebase.google.com/docs/reference/js/firebase.database.DataSnapshot#exists

Peter's answer is the correct solution. 彼得的答案是正确的解决方案。 I'm merely adding this for completeness. 我只是为了完整性而添加它。

When you call orderBy... on a Firebase reference, the database orders all child nodes on the key/value/child that you specify. 当您在Firebase参考上调用orderBy...时,数据库会对您指定的键/值/子项上的所有子节点进行排序。

If you then subsequently call startAt(...) on the query, it finds the (first) node that starts with that value and starts returning all results from there. 如果随后在查询上调用startAt(...) ,它会找到以该值开头的(第一个)节点并开始从那里返回所有结果。 So if you start at FKQLlqa , it will start returning keys at FKQLlqa and then return all keys after it. 所以如果你开始 FKQLlqa ,它将开始在返回键FKQLlqa ,然后后返回的所有密钥。

If you want to return the child node(s) with a specific key/value/child, you'd use equalTo(...) . 如果要返回具有特定键/值/子节点的子节点,则使用equalTo(...) So: 所以:

ref.orderByKey().equalTo(userUID).once("child_added", function(snapshot) {
  ... 

But as Peter said already, this is just a more expensive way to look up a child with a known key. 但正如彼得所说,这只是用一种已知钥匙查找孩子的一种更昂贵的方式。 I highly recommend using his better approach: ref.child(userUID).once("value" . 我强烈建议使用他更好的方法: ref.child(userUID).once("value"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM