Functions-samples: Firebase search by child value?

Created on 31 Oct 2017  路  4Comments  路  Source: firebase/functions-samples

Hello guys,

I am trying to get "pushToken" value by searching child value called "deviceUid". Since my query returning null, I cant send notification to user..

Here is my db and I cant get push token value with following code;

Thank you..

  const deviceUID = event.params.deviceUID;
  console.log("deviceUID: ", deviceUID);
  //deviceUID: 1f2d34d

  const getpushTokenPromise = admin.database().ref('/users').child('pushToken').orderByChild('deviceUid').equalTo(deviceUID).once('value');

  getpushTokenPromise.then(pushTokenSnapshot => {
    const token = pushTokenSnapshot.val();
    console.log("token:" ,token);
    //token: null
  });

image

Most helpful comment

here is a quick sample

firebase().database().ref('/app-users/users/').orderByChild("email").equalTo('[email protected]').on('value', function (snapshot) {
       //snapshot would have list of NODES that satisfies the condition
    console.log(snapshot.val())
        console.log('-----------');

       //go through each item found and print out the emails
       snapshot.forEach(function(childSnapshot) {

       var key = childSnapshot.key;
      var childData = childSnapshot.val();

        //this will be the actual email value found
         console.log(childData.email);
  });

});

in your case
_childData.email_ would be childData.pushToken
_orderByChild("email")_ would be orderByChild("deviceUid")

test

All 4 comments

here is a quick sample

firebase().database().ref('/app-users/users/').orderByChild("email").equalTo('[email protected]').on('value', function (snapshot) {
       //snapshot would have list of NODES that satisfies the condition
    console.log(snapshot.val())
        console.log('-----------');

       //go through each item found and print out the emails
       snapshot.forEach(function(childSnapshot) {

       var key = childSnapshot.key;
      var childData = childSnapshot.val();

        //this will be the actual email value found
         console.log(childData.email);
  });

});

in your case
_childData.email_ would be childData.pushToken
_orderByChild("email")_ would be orderByChild("deviceUid")

test

It looks like @davvit solved your problem before we could get to it. To make sure your query is efficient, also add this to your firebase database rules:

{
  "users": {
    "$userId": {
      ".indexOn": "deviceUid"
    }
  }
}

Hi @inlined , I'm just wondering what the database rule that you listed does in this situation?

It allows you to (efficiently) use orderByChild('deviceUid'). When the Firebase Realtime Database loads your node, it will keep a view of that data in deviceUid order.

Also, if you need complex querying, consider using Cloud Firestore.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

abhilash1in picture abhilash1in  路  4Comments

inglesuniversal picture inglesuniversal  路  5Comments

kaceo picture kaceo  路  3Comments

Midhilaj picture Midhilaj  路  5Comments

nhathiwala picture nhathiwala  路  4Comments