Make a query inside functions document - firebase

could you please tell me how to query those data? I have an array of ids and I want to pick their tokens from usersToken document… here is my code and database

image

Hi, @zeinabbadrazzam
Here you have shared a screenshot of your code, what is the problem exactly you are facing now and here you want tokens of ids=["505050","121212"] so, whether those ids are statically defined by you or you are getting dynamically from one of the databases. please provide this information so that I can help you.

ids are dynamicaly from the document “cancelledClasses”
i have made it as array to just test if i can bring any id token or not …
the problem is that i wanna put those ids token into an array and therefore put inside the " sendToDevice(tokens , payload)" function . in the code i used “parent and child” concept but i get a null array… could i share the full code ?

okay @zeinabbadrazzam as you want to make one array of the tokens first you need to fetch data from the userstoken database then you need to compare ids on that condition you can push tokens into your array. If you still not get my point then share your code.

it is okay to push one by one but i also don’t have the idea how i can do it as i still new in firebase.
here is my code:

var functions = require(‘firebase-functions’);
var admin = require(‘firebase-admin’);
admin.initializeApp(functions.config().firebase);
exports.pushNotification = functions.database.ref(’/cancelledClasses/{dsd}’).onWrite(change=> {
var test=change.data;
var ids=[“505050”,“121212”];
const message = change.after.val();
var tokens=
var rootSnapshot = change.after.ref.parent.parent;
for(i of ids){
rootSnapshot.child(“usersToken/”+i).once(“value”,snapshot => {
if (snapshot.exists()){
newArray.push(tokens.child(“token”).val());
}})}

    var payload = {
        
            "notification":{
                "click_action":"FCM_PLUGIN_ACTIVITY",
                "title":"From: Mr." + message.senderName,
                "body":message.course+" Class has been cancelled !",
                "sound":"default",
                "icon": 'https://goo.gl/Fz9nrQ'
                },
            "data":{
                "sender": "Mr." + message.senderName,
                "click_action":"FCM_PLUGIN_ACTIVITY",
                "title": message.course + " Cancelled Class",
                 "body": " The class of "+message.day + " has been cancelled."
            }
    }      
        return admin.messaging().sendToDevice(newArray, payload).then((response) => {
            console.log('Pushed notifications');
        }).catch((err) => {
            console.log(err);
        })

})

Try this:

var functions = require(‘firebase-functions’);
var admin = require(‘firebase-admin’);
admin.initializeApp(functions.config().firebase);
exports.pushNotification = functions.database.ref(’/cancelledClasses/{dsd}’).onWrite(change=> {
var test=change.data;
var ids=[“505050”,“121212”];
const message = change.after.val();
var tokens=[]
var rootSnapshot = change.after.ref.parent.parent;
rootSnapshot.child(“usersToken”).once(“value”,(snapshot) => {
 let tokens=snapshot.val();
for (let key in tokens){
   for (let i in ids){
      if(i == tokens[key]["ID"]{
       newArray.push(tokens[key]["ID"]);      
     }
   }
console.log("array",newArray);
}
}})}
1 Like

actually it is still not working…

var functions = require(‘firebase-functions’);
var admin = require(‘firebase-admin’);
admin.initializeApp(functions.config().firebase);
exports.pushNotification = functions.database.ref(’/cancelledClasses/{dsd}’).onWrite(change=>{
var test=change.data;
var ids=[“505050”,“121212”];
const message = change.after.val();
var newArray=
var tokens=
var rootSnapshot = change.after.ref.parent.parent;
rootSnapshot.child(“usersToken”).once(“value”,snapshot => {
let tokens=snapshot.val();
for (let key in tokens){
for (let i in ids){
if(i == tokens[key][“ID”]){
newArray.push(tokens[key][“token”]);
}}}})

    var payload = {
        
            "notification":{
                "click_action":"FCM_PLUGIN_ACTIVITY",
                "title":"From: Mr." + message.senderName,
                "body":message.course+" Class has been cancelled !",
                "sound":"default",
                "icon": 'https://goo.gl/Fz9nrQ'
                },
            "data":{
                "sender": "Mr." + message.senderName,
                "click_action":"FCM_PLUGIN_ACTIVITY",
                "title": message.course + " Cancelled Class",
                 "body": " The class of "+message.day + " has been cancelled."
            }
    }      
        return admin.messaging().sendToDevice(newArray, payload).then((response) => {
            console.log('Pushed notifications');
        }).catch((err) => {
            console.log(err);
        })

})

Hi, @zeinabbadrazzam

Have you checked data of newArray by console.log is it empty or not?

no, inside the catch part you mean? and where to view it ?

After pushing ids into newArray console it and check that the array is empty or it is filled with tokens

image image yes it is empty

see in my above answer I’ve edited my code and added console.log and ya also I’ve enclosed snapshot in braces run that and please check in the console part of inspect it’ll return either empty array or array filled with tokens.

taaaaaaaaanks alot
the code wasn’t working because I didn’t have any access in tokens outside the function getToken(), so it was every time return null array, but now I solved it and the code is working correctly.

I’m really sorry sir for wasting your time.:smiley:
thank you

1 Like