简体   繁体   English

无法访问Firebase orderByChild查询结果中的值

[英]Cannot access value in firebase orderByChild query result

Using Firebase JS library. 使用Firebase JS库。 Trying to access value in firebase query result with no success: 尝试访问Firebase查询结果中的值没有成功:

  var ref = firebase.database().ref(`/listUser/${userId}/`);

  ref.orderByChild('email').equalTo('myemail@myemail.com')
  .once("value", 
  res => {
  console.log(res.val());
  console.log(res.key);
  })

The first console.log returns: 第一个console.log返回:

{R74E2nc: {email: "myemail@myemail.com", userKey: "R74E2nc"}}

The second console.log returns the userId that I pass in the reference: 第二个console.log返回我在引用中传递的userId:

BKjAJm

The value I would like to get is R74E2nc because this is the one I don't know and need for further operations. 我想获得的值是R74E2nc,因为这是我不知道的并且需要进一步操作。 I tried res.val().key and res.val().userKey but they are undefined. 我尝试使用res.val()。key和res.val()。userKey,但它们是未定义的。

Database structure is: 数据库结构为:

"listUser": {
     "BKjAJm": {
        "R74E2nc": {email: "myemail@myemail.com", userKey: "R74E2nc"}
                },
                {
        "x1x22": {email: "my2@my2.com", userKey: "x1x22"}

                }
             } 

How can I access this value "R74E2nc"? 如何访问该值“ R74E2nc”?

Thank you. 谢谢。

You can try this code, by replacing "value" with "child_added" 您可以尝试通过将“ value”替换为“ child_added”来尝试这段代码

 var userz = firebase.database().ref()
    .orderByChild("email")
    .equalTo('myemail@myemail.com')
    .once("child_added", function(snapshot) {
        ...
    });

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

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