在运行嵌套查询的嵌套对象上使用Firebase Cloud函数搜索数据时,未指定索引 [英] unspecified index when searching data with firebase cloud function on nested object running nested Query

查看:82
本文介绍了在运行嵌套查询的嵌套对象上使用Firebase Cloud函数搜索数据时,未指定索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using fire-base to retrieve nested data of users node, and while running query I'm facing this issue fetching data from fire-base database.

考虑在以下位置添加".indexOn":"userId" /users/YJdwgRO08nOmC5HdEokr1NqcATx1/1/following/users to your security 规则,以获得更好的性能.

Consider adding ".indexOn": "userId" at /users/YJdwgRO08nOmC5HdEokr1NqcATx1/following/users to your security rules for better performance.

数据库结构:

 "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)

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

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

I think the problem is with this query , My first query on following node is returning me data but when I'm retrieving data of its parent node user, I'm getting warning.

我尝试使用functions.database.ref,但是它给了我下面的异常.

I tried to use functions.database.ref but it gave me below exception.

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

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

Firebase Functions, admin.database().ref(…) is not a function

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

Firebase Functions, functions.database().ref(…) is not a function

推荐答案

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

You are getting the wrong reference for reading the user. You need to do the following to get correct reference: snap.ref.parent.parent.parent (ref now will be at /users). You query is trying to read the users node under following.

警告是您需要编写firebase规则,该规则可基于userId启用索引,否则操作将占用大量带宽.

The warning is that you need to write the firebase rule which enables the indexing based on userId, otherwise the operation will be bandwidth expensive.

这是将添加索引的规则:

here is the rule which will add indexing:

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

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

Here is the resource for more information on Database rules : https://firebase.google.com/docs/database/security/

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

This is a simple tool by firebase to write rules easily: https://github.com/firebase/bolt

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

P.S: you are returning two promises your last promise for sending notification will not work. Nest or chain it with the firebase query. Also make your query single value event with changing on('child_changed') to once('value').

这篇关于在运行嵌套查询的嵌套对象上使用Firebase Cloud函数搜索数据时,未指定索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆