在运行嵌套查询的嵌套对象上使用 firebase 云函数搜索数据时未指定索引

2023-12-24

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

考虑添加 ".indexOn": "userId" /users/YJdwgRO08nOmC5HdEokr1NqcATx1/following/users 为了您的安全 规则以获得更好的性能。

数据库结构:

 "users" : {
    "1vWvSXDQITMmKdUIY7SYoLA1MgU2" : {
      "userEmail" : "[email protected] /cdn-cgi/l/email-protection",
      "userId" : "1vWvSXDQITMmKdUIY7SYoLA1MgU2",
      "userName" : "Malik Abdul Kawee",
      "userPhoneNumber" : "",
      "userProfileImage" : "https://pbs.twimg.com/profile_images/1018741325875867648/ZnKeUiOJ_400x400.jpg"
    },
    "YJdwgRO08nOmC5HdEokr1NqcATx1" : {
      "following" : {
        "1vWvSXDQITMmKdUIY7SYoLA1MgU2" : {
          "currentFollowingUserId" : "YJdwgRO08nOmC5HdEokr1NqcATx1",
          "userEmail" : "[email protected] /cdn-cgi/l/email-protection",
          "userId" : "1vWvSXDQITMmKdUIY7SYoLA1MgU2",
          "userName" : "Malik Abdul Kawee",
          "userPhoneNumber" : "",
          "userProfileImage" : "https://pbs.twimg.com/profile_images/1018741325875867648/ZnKeUiOJ_400x400.jpg"
        }
      },
      "userEmail" : "[email protected] /cdn-cgi/l/email-protection",
      "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(…) 不是函数


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

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

这是添加索引的规则:

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

以下是有关数据库规则的更多信息的资源:https://firebase.google.com/docs/database/security/ https://firebase.google.com/docs/database/security/

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

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在运行嵌套查询的嵌套对象上使用 firebase 云函数搜索数据时未指定索引 的相关文章

随机推荐