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
});

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

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.
Most helpful comment
here is a quick sample
in your case
_childData.email_ would be childData.pushToken
_orderByChild("email")_ would be orderByChild("deviceUid")