简体   繁体   English

Firebase 云函数:“解析 function 触发器时出错”

[英]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.

相关问题 Cloud Functions,Firebase:解析函数触发错误 - Cloud Functions, Firebase: parsing function triggers error firebase部署错误:“解析函数触发器时发生错误” - firebase deployment error:“error occured while parsing your function triggers” Cloud Functions for Firebase firebase 部署错误:“解析触发器时出错:找不到模块‘firebase-admin’” - Cloud Functions for Firebase firebase deploy error: "Error parsing triggers: Cannot find module 'firebase-admin'" Firebase-节点云功能错误解析触发器:找不到模块“ firebase-functions” - Firebase - Node Cloud Functions Error parsing triggers: Cannot find module 'firebase-functions' Firebase 云功能:部署 function 时出错 - Firebase Cloud Functions: Error while deploying function 获取 `eslint' - 解析错误,同时编译 firebase 云 function - Getting `eslint' - parsing error, while compiling firebase cloud function 错误:解析触发器错误:找不到模块“ firebase-functions” - Error: Error parsing triggers: Cannot find module 'firebase-functions' Firebase HelloWorld的云功能“错误:发生意外错误。” - Cloud Functions for Firebase HelloWorld “Error: An unexpected error has occurred.” 包括异步 Function Firebase Cloud Functions 内(eslint“解析错误:意外的令牌函数”) - Including Async Function Within Firebase Cloud Functions (eslint "Parsing error: Unexpected token function") Firebase 云 Function - 解析错误:意外令牌 => - Firebase Cloud Function - Parsing error: Unexpected token =>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM