簡體   English   中英

Firebase 雲函數:“解析 function 觸發器時出錯”

[英]Firebase Cloud Functions: 'Error occurred while parsing your function triggers'

我正在嘗試編寫和部署 firebase CF 來收聽“帖子”集合中的文檔創建並發送通知。 下面是我寫的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',
                    }
                }
            );
        })
    })
})

但是,當我嘗試部署 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)

不知道我哪里出錯了,因為其他所有的功能都部署和運行沒有任何問題,而且這種情況下的await命令似乎沒有錯。

有人可以建議這可能是什么根本原因嗎?

受@samthecodingman 評論的啟發,我編輯了代碼,下面的代碼運行良好。 謝謝!

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM