【问题标题】:Firebase realtime db query showing undefined?Firebase 实时数据库查询显示未定义?
【发布时间】:2021-04-08 10:40:40
【问题描述】:

我的 Firebase 控制台中有以下数据。

Firebase updated console image here

下面是上图的json形式。

"Messages" : {
    "fYIM9pgpGdUpXtgrC3CavJyoN" : {
           "1FH4baB4LLeKhPjUqiGvj5T6DU" : {
              "chat": {// all chat here
               },
              "chattingid" : {
                    "chatid" : "1FH4baB4LLeKhPjUqiGvj5T6DU"
                }
            },
            "TCAv2NtQYdUXkF2COgZslsXARh" : {
                "chat": {//all chat here
                 },
                "chattingid" : {
                        "chatid" : "TCAv2NtQYdUXkF2COgZslsXARh"
                    }
                },
            }
            
            
}

我正在使用以下查询来获取所有 chatid 并在我的控制台中两次未定义。

firebase.database().ref("Messages/"+suid).orderByChild("chattingid").on("child_added", function(data){
       
    console.log("You are here to see "+ JSON.stringify(data.val().chatid));
        
})   

如何使用 firebase 查询获取所有 chatid。是否可以?谢谢

【问题讨论】:

  • 您在问题中包含了 JSON 树的图片。请将其替换为实际的 JSON 作为文本,您可以通过单击 your Firebase Database console 的溢出菜单 (⠇) 中的 Export JSON 链接轻松获得。将 JSON 作为文本使其可搜索,让我们可以轻松地使用它来测试您的实际数据并在我们的答案中使用它,总的来说这只是一件好事。
  • 我有一段时间没有使用 firebase,但我倾向于说你应该用 .orderByChild("chattingid/chatid") 替换你的 .orderByChild("chattingid"),因为 chattingid 是一个对象并且您可能希望按 chatid 中的实际文本进行排序。另外,欢迎来到 SO!
  • @user999 跟进@raphael 的回答,当您不确定为什么看到您所看到的内容时,最好的办法就是打印出data 并缩小从那里下来。如果data 为您提供了一个数组,那就太好了,您可以从那里开始工作,并在迭代步骤中使用console.log 来获得您的目标。如果 data 未定义,那么您知道您的 firebase 查询中有一些需要解决的问题。
  • 嗨 @FrankvanPuffelen Json 添加了。
  • 好消息@nviens。嵌套属性确实需要在orderByChild() 调用中指定。你可能想把它写下来作为答案,这样我们就可以投票了。 ??????

标签: javascript firebase firebase-realtime-database


【解决方案1】:

数据以数组形式返回,因此您可以使用 forEach 来获取 chatid 的

firebase.database().ref("Messages/" + suid).orderByChild("chattingid").on("child_added", data => {
            data.forEach(snapshot => {
                console.log("You are here to see " + snapshot.val().chatid);
            })
 })

【讨论】:

  • 嗨@raphael。出现错误没有为 chattingid 定义索引。请进一步指导如何在 firebase 控制台中对其进行索引。谢谢
【解决方案2】:

张贴我的 cmets 作为答案。我有一段时间没有使用 firebase,但我倾向于说你应该用.orderByChild("chattingid/chatid") 替换你的.orderByChild("chattingid"),因为 chattingid 是一个对象,你可能想按 chatid 中的实际文本进行排序。

所以我建议从类似于以下内容开始:

firebase.database().ref("Messages/"+suid).orderByChild("chattingid/chatid").on("child_added", function(data){
    console.log("You are here to see ", data.val());
})

当您不确定为什么会看到您所看到的内容时,最好的办法就是打印出数据并从那里缩小范围。

如果数据为您提供了一个数组,那就太好了,您可以从那里开始工作,并在迭代步骤中使用 console.log console.log(JSON.parse(data));console.log(JSON.stringify(data)); 来获得您的目标。如果数据未定义,那么您知道您的 Firebase 查询中有一些需要解决的问题

编辑

您可以考虑尝试执行以下操作来获取 chatid,因为通过您的查询,您正在获取“Messages/fYIM9pgpGdUpXtgrC3CavJyoN”并且结果值应该类似于以下内容。

  "1FH4baB4LLeKhPjUqiGvj5T6DU" : {
   "chat":{// all chat here}
    "chattingid" : {
      "chatid" : "1FH4baB4LLeKhPjUqiGvj5T6DU"
    }
  },
  "TCAv2NtQYdUXkF2COgZslsXARh" : {
    "chat":{// all chat here}
    "chattingid" : {
      "chatid" : "TCAv2NtQYdUXkF2COgZslsXARh"
    }
  },

键与 chatids 字段匹配,因此您可以执行以下操作来获取 ids(或者特别是返回的 json 对象中的键,它应该与 chatids 字段匹配)。

firebase.database().ref("Messages/"+suid).orderByChild("chattingid/chatid").on("child_added", function(data){
    console.log("You are here to see ",data.val());
}), function (error) {
    console.log("Error: " + error.code);
});

EDIT 2 -- 更新 -- 为您的数据库添加索引

我遇到了以下 SO 答案 - https://stackoverflow.com/a/27878176/3272438

您可以尝试将以下索引规则添加到 chatid 索引

{
  "rules": {
    "Messages": {
      "chattingid": {
         ".indexOn": ["chatid"]
      }
    }
  }
}

查看以下教程,了解如何处理返回的数据 - https://www.tutorialspoint.com/firebase/firebase_event_types.htm

编辑 3

因为上面的 EDIT 2 向控制台提供了 Array [ "node_", "ref_", "index_", ],所以该代码显然没有解决问题。

我利用了我在React, Firebase: Access values of JSON and also get key value 中遇到的一些示例以及我所做的更改以及@raphael 对循环返回的对象(可能是一个数组)的回答,以得出以下代码。

尝试以下方法:

firebase.database().ref("Messages/" + suid).orderByChild("chattingid/chatid").on("child_added", data => {
            data.forEach(snapshot => {
                console.log("just the val " , snapshot.val());
                console.log("val.chatid " , snapshot.val().chatid);
                console.log("val.chattingid.chatid " , snapshot.val().chattingid.chatid);
            })
 })

【讨论】:

  • 我现在在我的控制台中获取所有数据(如我的问题中的图像所示)。我只想得到所有的chatid。
  • 您正在获取所有数据,因为这就是您所要求的 firebase.database().ref("Messages/"+suid)orderByChild("chattingid/chatid") 唯一要做的就是为您排序列表。如果您只想获取聊天 ID,那么我的猜测是您应该迭代返回的数据,类似于 @raphael 所做的那样。我会先做data.forEach(snapshot => { console.log('snapshot', snapshot) })
  • 我已经尝试过 raphael 的解决方案,但出现索引错误。你能指导我如何在firebase实时数据库规则中索引它。谢谢
  • 我用一个潜在的解决方案更新了我的答案,用于根据返回的数据显示键。这将是一个客户端解决方案。我必须重新学习一些firebase db规则才能在这方面提供帮助......虽然我现在不能这样做。让我知道这是否能让你更进一步。
  • @user999 我用你可以尝试使用的潜在索引规则更新了我的答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-31
  • 1970-01-01
  • 2020-08-08
  • 1970-01-01
  • 2022-06-10
  • 2018-09-14
相关资源
最近更新 更多