I have a Javascript Cloud Function as below, and when I do Firebase deploy, I'm getting parsing error. I'm complaining about line const response = await admin.messaging().sendToDevice(tokens, payload);
'use strict';
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
const db = admin.firestore();
exports.sendFollowerNotification = functions.database.ref('/orders/{order_id}')
.onWrite(async (change, context) => {
// get vars
const order_id = context.params.order_id;
console.log('order_id:', order_id);
const uid = change.after.data().uid;
console.log('uid:', uid);
// get user and send notifications
const docRef = db.collection('users').doc(uid);
const getDoc = docRef.get()
.then(doc => {
const fcmToken = doc.data().fcmToken;
console.log('fcmToken:', fcmToken);
const payload = {
notification: {
title: 'We have received your order!',
body: `Thank you, we have received order ${order_id}`
}
};
const response = await admin.messaging().sendToDevice(tokens, payload);
console.log('response:', response);
});
});
Error:
35:37 error Parsing error: Unexpected token admin
✖ 1 problem (1 error, 0 warnings)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! functions@ lint: `eslint .`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the functions@ lint script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
You are using await
without declaring the callback function as async
. That's why you are getting a parsing error since await
in of itself is irrelevant without having the parent function declared asynchronous. Try writing docRef.get()
like such:
const getDoc = docRef.get()
.then(async doc => {
const fcmToken = doc.data().fcmToken;
console.log('fcmToken:', fcmToken);
const payload = {
notification: {
title: 'We have received your order!',
body: `Thank you, we have received order ${order_id}`
}
};
const response = await admin.messaging().sendToDevice(tokens, payload);
console.log('response:', response);
});
The only difference here is that I added async
before the callback inside the .then()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.