[英]Firebase Cloud Functions: 'Error occurred while parsing your function triggers'
I am trying to write and deploy a firebase CF to listen to document creation in 'posts' collection and send notifications.我正在尝试编写和部署 firebase CF 来收听“帖子”集合中的文档创建并发送通知。 Below is the function I wrote.下面是我写的function。
const functions = require('firebase-functions');
const db = admin.firestore();
//
exports.myFunction = functions.firestore
.document('posts').onCreate((snap,context) => {
const postdata = snap.data();
const postid = postdata.postid;
return db.collection('subscription').doc(postid).get()
.then(doc=>{
const subsuid = doc.data().subs_uid;
subsuid.forEach(sb=>{
const tokendata = (await db.collection('tokens').doc(sb).get()).data();
await admin.messaging().sendMulticast(
{
tokens:tokendata.token, // ['token_1', 'token_2', ...]
notification: {
title: 'title',
body: 'text'
},
apns: {
headers: {
'apns-priority': '10',
},
payload: {
aps: {
contentAvailable: true
}
}
},
android: {
priority: 'high',
}
}
);
})
})
})
However, when I try to deploy the function, the system returns below error:但是,当我尝试部署 function 时,系统返回以下错误:
Error: Error occurred while parsing your function triggers.
/path_to_my_function
const tokendata = (await db.collection('tokens').doc(sb).get()).data();
^^
SyntaxError: Unexpected identifier
at wrapSafe (internal/modules/cjs/loader.js:1053:16)
at Module._compile (internal/modules/cjs/loader.js:1101:27)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1157:10)
at Module.load (internal/modules/cjs/loader.js:985:32)
at Function.Module._load (internal/modules/cjs/loader.js:878:14)
at Module.require (internal/modules/cjs/loader.js:1025:19)
at require (internal/modules/cjs/helpers.js:72:18)
at /usr/local/lib/node_modules/firebase-tools/lib/triggerParser.js:15:15
at Object.<anonymous> (/usr/local/lib/node_modules/firebase-tools/lib/triggerParser.js:53:3)
at Module._compile (internal/modules/cjs/loader.js:1137:30)
Don't know where I went wrong, because all the other functions are deployed and run without any problem, and the await command in this case doesn't seem wrong.不知道我哪里出错了,因为其他所有的功能都部署和运行没有任何问题,而且这种情况下的await命令似乎没有错。
Can someone advise on what might be the root cause of this?有人可以建议这可能是什么根本原因吗?
Inspired by @samthecodingman 's comment, I edited the code and the below code works just fine.受@samthecodingman 评论的启发,我编辑了代码,下面的代码运行良好。 Thanks!谢谢!
exports.myFunction = functions.firestore
.document('posts').onCreate((snap,context) => {
const postdata = snap.data();
const postid = postdata.postid;
return db.collection('subscription').doc(postid).get()
.then(doc=>{
const subsuid = doc.data().subs_by_users_uid;
const qSubsuid = subsuid.map(sb=>db.collection('tokens').doc(sb).get());
return Promise.all(qSubsuid)
.then(results=>{
results.forEach(doc=>{
return admin.messaging().sendMulticast(
{
tokens:doc.data().token, // ['token_1', 'token_2', ...]
notification: {
title: 'title',
body: 'body'
},
apns: {
headers: {
'apns-priority': '10',
},
payload: {
aps: {
contentAvailable: true
}
}
},
android: {
priority: 'high',
}
}
);
})
})
})
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.