【问题标题】:unspecified index when searching data with firebase cloud function on nested object running nested Query在运行嵌套查询的嵌套对象上使用 Firebase 云功能搜索数据时未指定索引
【发布时间】:2019-01-22 01:04:13
【问题描述】:

我正在使用 fire-base 检索用户节点的嵌套数据,在运行查询时,我遇到了从 fire-base 数据库中获取数据的问题。

考虑添加 ".indexOn": "userId" at /users/YJdwgRO08nOmC5HdEokr1NqcATx1/following/users 保护您的安全 更好的性能规则。

数据库结构:

 "users" : {
    "1vWvSXDQITMmKdUIY7SYoLA1MgU2" : {
      "userEmail" : "test2kawee@gmail.com",
      "userId" : "1vWvSXDQITMmKdUIY7SYoLA1MgU2",
      "userName" : "Malik Abdul Kawee",
      "userPhoneNumber" : "",
      "userProfileImage" : "https://pbs.twimg.com/profile_images/1018741325875867648/ZnKeUiOJ_400x400.jpg"
    },
    "YJdwgRO08nOmC5HdEokr1NqcATx1" : {
      "following" : {
        "1vWvSXDQITMmKdUIY7SYoLA1MgU2" : {
          "currentFollowingUserId" : "YJdwgRO08nOmC5HdEokr1NqcATx1",
          "userEmail" : "test2kawee@gmail.com",
          "userId" : "1vWvSXDQITMmKdUIY7SYoLA1MgU2",
          "userName" : "Malik Abdul Kawee",
          "userPhoneNumber" : "",
          "userProfileImage" : "https://pbs.twimg.com/profile_images/1018741325875867648/ZnKeUiOJ_400x400.jpg"
        }
      },
      "userEmail" : "test2atif@gmail.com",
      "userId" : "YJdwgRO08nOmC5HdEokr1NqcATx1",
      "userName" : "Atif AbbAsi",
      "userPassword" : "test123",
      "userPhoneNumber" : "",
      "userProfileImage" : "http://paperlief.com/images/enrique-iglesias-body-workout-wallpaper-4.jpg"
    }
  }

数据库规则:

    "users": {
     ".indexOn":  ["userId","currentFollowingUserId",".value"],
       "$userId": {
         "following": {
        //"$userId": {
             ".indexOn":  ["userId","currentFollowingUserId",".value"]
        }
    //}
       } 
}

函数查询:

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);


exports.sendFollowingNotifications = functions.database.ref('/users/{userId}/following/{followingId}')
       //.onWrite(event => {
         .onCreate((snap,context) => {  


        console.info("Child value is val() " ,snap);


        var childNodeValue=snap.val();

        var topic=childNodeValue.userId;

        //var ref = firebase.database().ref.child('users');

        //console.log("testing ref pathName : " ,snap.ref.parent.parent.parent.pathname);
    //  console.log("testing ref : " ,snap.ref.parent.parent.parent.path);

        //var ref = admin.database().ref("users");

        //.child('users')

        return snap.ref.parent.parent.parent.orderByChild("userId").equalTo(childNodeValue.currentFollowingUserId)

     // .on('child_changed').then(snapshot => { once('value')
         .once('value', function(snapshot){ 
        var parentNodeValue=snapshot.val();

        console.info("Topic ID " ,topic);

        console.info("Parent value is val() " ,snapshot.val());

              var payload = {
            data: {
                username: parentNodeValue.userName,
                imageurl:parentNodeValue.userProfileImage,
                description:"Started Following You"
            }
        };



           // Send a message to devices subscribed to the provided topic.
        return admin.messaging().sendToTopic(topic, payload)
            .then(function (response) {
                // See the MessagingTopicResponse reference documentation for the
                // contents of response.
                console.log("Successfully sent message:", response);
                return response;
            })
            .catch(function (error) {
                console.log("Error sending message:", error);
                return error;
            });

      });








      });

返回 snap.ref.parent.child('users').orderByChild("userId").equalTo(childNodeValue.currentFollowingUserId)

我认为问题出在这个查询上,我在以下节点上的第一个查询正在返回数据,但是当我检索其父节点用户的数据时,我收到了警告。

我尝试使用functions.database.ref,但它给了我以下异常。

so I tried using this `snap.ref.parent.`to get reference of parent node.

Firebase 函数,admin.database().ref(…) 不是函数

Firebase 函数,functions.database().ref(…) 不是函数

【问题讨论】:

  • 错误消息末尾有一个users,在您的 JSON 和规则中缺失。您似乎在查询不存在的数据。
  • @FrankvanPuffelen 我认为问题出在这条线 return snap.ref.parent.child('users') 从以下节点获取数据后,我正在尝试获取其父节点,但我不知道如何为了获得父父节点,我只是尝试使用 snap.ref.parent 。
  • snap.ref.parent.parent?虽然我认为您可能正在寻找snap.ref.root。有关 DatabaseReference 的所有属性,请参阅参考文档:firebase.google.com/docs/reference/admin/node/…
  • 感谢@FrankvanPuffelen snap.ref.parent.parent.parent 为我工作。

标签: javascript node.js firebase firebase-realtime-database


【解决方案1】:

您在阅读用户时得到了错误的参考。您需要执行以下操作以获得正确的参考:snap.ref.parent.parent.parent(参考现在将位于 /users)。您的查询正在尝试读取以下用户节点。

警告是你需要编写firebase规则来启用基于userId的索引,否则操作会占用带宽。

这里是添加索引的规则:

"users": {
   "$uid" : {
     ".indexOn" : ["userId"]
   }
}

这里是有关数据库规则的更多信息的资源:@​​987654321@

这是firebase的一个简单工具,可以轻松编写规则:https://github.com/firebase/bolt

P.S:您将返回两个承诺,您最后一个发送通知的承诺将不起作用。使用 firebase 查询嵌套或链接它。还可以通过将on('child_changed') 更改为once('value') 来进行查询单值事件。

【讨论】:

  • 看看数据库结构 umar..我正在使用嵌套查询,问题在于 return snap.ref.parent.child('users') 这个查询。
  • 查看我的更新答案。您在函数内部查询时引用错误。
  • 有没有办法将引用打印为字符串。?比如***/用户/值
  • ref 是 firebase sdk 中的一个 Reference 接口,有一个变量 path 来作为路径获取参考。
  • 发送消息时出错:{ 错误:消息负载包含“data.userName”属性的无效值。值必须是字符串。如何解决这个问题?
猜你喜欢
  • 1970-01-01
  • 2016-03-27
  • 1970-01-01
  • 2020-02-25
  • 1970-01-01
  • 1970-01-01
  • 2021-05-13
  • 2021-12-19
  • 2015-06-29
相关资源
最近更新 更多