Reputation: 519
I am trying to deploy the following firebase function. I keep getting the error unexpected token doc
but my forEach loop is based on the google documentation. What am I doing wrong?
exports.sendRoomNotification = functions.firestore.document('/Rooms/{room}/messages/{message}').onCreate(async (snap, context) => {
// get child data
const ref = snap.ref;
const childDoc = await ref.get();
const childUsername = childDoc.data().senderId;
// get parent data
const roomRef = ref.parent.parent;
const parentDoc = await roomRef.get();
const parentName = parentDoc.data().name
console.log('parent.name => ', parentName);
// send message
const tokenRef = roomRef.collection('members')
const tokenSnapshot = tokenRef.get()
tokenSnapshot.forEach(doc => {
if doc.data().notify {
var message = {
notification: {
title: 'Prizm',
body: `@${childUsername} send a message in ${parentName}`,
badge: '1',
sound: 'default'
},
token: doc.data().fcmToken
}
let response = await admin.messaging().send(message);
console.log(response);
}
});
})
Upvotes: 0
Views: 93
Reputation: 1405
If the error was on the line if doc.data().notify {
, wrap the condition within ()
. Or else in NodeJS it's considered as syntax error. Ex,
if (doc.data().notify) {
this will resolve the issue. If it's not in that line, try to get & update full error logs.
Upvotes: 1