Firebase部署时出现Firebase云函数(Javascript)解析错误



我有一个Javascript云函数,如下所示,当我部署Firebase时,我会遇到解析错误。我在抱怨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);
});
});

错误:

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.

您使用的是await,而没有将回调函数声明为async。这就是为什么会出现解析错误的原因,因为如果没有声明异步的父函数,await本身是不相关的。试着像这样写docRef.get()

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);
});

这里唯一的区别是,我在.then()内部的回调之前添加了async

相关内容

  • 没有找到相关文章

最新更新