1
votes

I have two collections. one is jobs and other one is users.

User can select favourite categoryName. I store on User collection... Inside job I store all categories with categoryName.

When inserting new job to thejob and this job categoryName == user selected categoryName I triggered push notification to device.

Just to know: I store device token tokens collection.

Error Message coming on the Function log when insert newjob'

TypeError: favoriteData is not iterable at exports.favoriteTrigger.functions.firestore.document.onCreate (/srv/index.js:57:25) at cloudFunction (/srv/node_modules/firebase-functions/lib/cloud-functions.js:131:23) at /worker/worker.js:825:24 at at process._tickDomainCallback (internal/process/next_tick.js:229:7)

exports.favoriteTrigger = functions.firestore.document('jobs/{any}')
.onCreate((snapshot,context)=>{
    var categoryName = [];
    var equalCategory = [];
    if(snapshot.empty){
        console.log('No Jobs in Jobs Collection');
        return null;
    }else{
        favoriteData = snapshot.data();
        for(var jobC of favoriteData){ //57:25 line code. error is coming here
            categoryName.push(jobC.data().categoryName);
        }
    admin.firestore().collection('users').get().then((snapshot)=>{
        var userCategory = [];
        if(snapshot.empty){
            console.log('User not select favorite category yet');
            return null;
        }else{
            for(var jobCategory of snapshot.docs){
                userCategory.push(jobCategory.data().jobCategory);
            }
            var i;
            var j;
            for(i = 0; i < userCategory.length; i++){
                for(j = 0; j < categoryName.length; j++){
                    if(userCategory[i] === categoryName[j]){
                        return equalCategory.push(userCategory[i]);
                    }
                }
            }
            return null;
        }
    }).catch((error)=>{
        console.log('Error last ' + error);
    });
        admin.firestore().collection('tokens').get().then((snapshot)=>{
            var tokens = [];
            if(snapshot.empty){
                console.log('No devices');
                return null;
            }else{
                for(var token of snapshot.docs){
                    tokens.push(token.data().tokenId);
                }

                var payload = {
                    "notification":{
                        "title":"You have new job ",
                        "body":"Offer",
                        "sound":"default"
                    },
                    "data":{
                        "sendername":msgData.applyJobDate,
                        "message":msgData.jobId
                    }
                };
                if(equalCategory.length !== 0){
                    return admin.messaging().sendToDevice(tokens,payload).then((response)=>{
                        console.log('pushed them all');
                        return null;
                    }).catch((error)=> {
                        console.log(error);
                        return null;
                    })
                }
            }
            return null;
        }).catch((error)=>{
            console.log(error);
            return null;
        })
}
});
1
Did it change from .val() on snapshot? - Anthony
I try now.. TypeError: snapshot.val(...) is not a function or its return value is not iterable at exports.favoriteTrigger.functions.firestore.document.onCreate - BloodLoss
Ah I see - .val() is for firebase database - Anthony
I modified my question description please check. Do you understand it? - BloodLoss

1 Answers

4
votes

DocumentSnapshot .data() returns an object, which you can't iterate over using for...of. Depending on what you're trying to do, you could use:

  • for...in
  • Object.keys()
  • Object.values()

on the returned object