firebase使用orderByKey()和equalTo()检索数据

iam*_*007 6 firebase

所以我试图在这个结构中找到数据"aaaaabbbbbbaaaa":

disney
 studentList
 -Jh46tlaNFx_YmgA8iMJ: "aaaaabbbbbbaaaa"
 -Jh474kAvoekE4hC7W3b:"54ce1cbec4335cd3186105dc"
Run Code Online (Sandbox Code Playgroud)

我用过这个

var ref = new Firebase("https://disney.firebaseio.com/");
ref.child("studentList").orderByKey().equalTo("54ca2c11d1afc1612871624a").on("child_added", function(snapshot) {
  console.log(snapshot.val());
});
Run Code Online (Sandbox Code Playgroud)

但我没有得到任何回报.哪里错了?我该怎么用?

Fra*_*len 19

在你已经给出的数据样本,没有在任何节点studenList关键54ca2c11d1afc1612871624a.

你的钥匙是-Jh46tlaNFx_YmgA8iMJ-Jh474kAvoekE4hC7W3b.您可以通过以下方式轻松确定:

ref.child("studentList").orderByKey().on("child_added", function(snapshot) {
  console.log(snapshot.key()); // on newer SDKs, this may be snapshot.key
});
Run Code Online (Sandbox Code Playgroud)

您似乎希望按其值对节点进行排序,但这不是Firebase目前可用的操作.Firebase只能通过命名属性的值,密钥或其优先级查询子.因此,如果您将数据结构更改为:

disney
 studentList
   -Jh46tlaNFx_YmgA8iMJ:
     name: "aaaaabbbbbbaaaa"
   -Jh474kAvoekE4hC7W3b:
     name: "54ce1cbec4335cd3186105dc"
Run Code Online (Sandbox Code Playgroud)

您可以通过名称获取子订单:

ref.child("studentList")
   .orderByChild("name")
   .equalTo("54ca2c11d1afc1612871624a")
   .on("child_added", function(snapshot) {
      console.log(snapshot.val());
    });
Run Code Online (Sandbox Code Playgroud)

只是一个侧节点:如果您的实际节点值与您提供的示例中的节点值类似,您可能需要考虑使用这些值作为键; 他们看起来对我未经训练的眼睛来说已经非常独特.