0
votes

I'm building an mobile app using flutter, and I want to make it support automated push up notifications then I used firebase cloud messaging. The function that I wrote it :

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
//admin.initializeApp();
var msgData;
exports.offerTrigger = functions.firestore.document('requests/{requestId}'
).onCreate((snapshot,context) => {
    msgData = snapshot.data();
    admin.firestore().Collection('pushtokens').get().then((snapshots) => {
        var tokens = [];
        if(snapshots.empty){
            console.log('No devices');
        }
        else {
            for(var token of snapshots.docs){
                tokens.push(token.data().deviceId);
            }
            var payload = {
                "notification" : {
                    "title" : "From" + msgData.Name,
                    "body"  : "Request " + msgData.requestDetial,
                    "sound" : "default"
                },
                "data" : {
                    "SenderName" : msgData.Name,
                    "message"    : msgData.requestDetial
                }
            }
            admin.messaging().sendToDevice(tokens,payload).then((respone) => {
                console.log("pushed them all");
            }).catch((err) => {
                console.log(err);
            });
        }
    })
})

after I deployed my function, and when I add some documents I got an error in firebase functions log the error :

TypeError: admin.firestore(...).Collection is not a function

at exports.offerTrigger.functions.firestore.document.onCreate (/user_code/index.js:9:23)

at cloudFunctionNewSignature (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:114:23)

at cloudFunction (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:144:20)

at /var/tmp/worker/worker.js:827:24

at process._tickDomainCallback (internal/process/next_tick.js:135:7)

How can I fix this error?

1

1 Answers

0
votes

Change this:

    admin.firestore().Collection('pushtokens').get().then((snapshots) => {

into this:

   admin.firestore().collection('pushtokens').get().then((snapshots) => {

from the docs:

collection(collectionPath)

Gets a CollectionReference instance that refers to the collection at the specified path.