0
votes

i'm have this code in my index.js to trigger when i create a data to cloud firestore, other user get a notification. but it's not work

const functions = require('firebase-functions'); const admin = require('firebase-admin');

admin.initializeApp();

const db = admin.firestore();
const fcm = admin.messaging();


exports.sendNotification = functions.firestore
    .document('donorRequests').
    onCreate(async snapshot => {
        const req = snapshot.data();
        var user = db.collection('account/{uid}').get()
        if((db.collection('accounts/{uid}/isVolunteer').get()===true)
            &&(db.collection('account/{uid}/bloodType').get()===req.bloodType)
            &&(db.collection('account/{uid}/rhesus').get()===req.rhesus)
            &&(db.collection('account/latestDonor').get()-Date.now()>54)){
            const payload = {
                "notification":{
                    title: 'Hai Pahlawan!',
                    body: 'Seseorang butuh kamu, yuk kita bantu :)',
                    clickAction: 'FLUTTER_NOTIFICATION_CLICK'
                }
            };
            return fcm.sendAll(payload);
        }
    });
1
Any error messages in function logs? Also did Aqil's answer help?Emil Gi

1 Answers

0
votes

Your Firestore cloud function trigger has many mistakes.

First of all in your usage of collection, it should only hold a collection_name as a string, and doc contains some document path after the stated collection. For example to get a document with uid1 from the accounts collection, it is done like this

Method1

db.collection("accounts").doc("uid1").get()

Method2

db.doc("accounts/uid1").get()

Secondly, these 2 methods access a document so they will return a Promise which resolves to a DocumentSnapshot in the documentation, and you have to add an await keyword in front of them, something like

const record = await db.collection("accounts").doc("uid1").get();