Firebase Cloud Functions error_:未定义变量

时间:2019-05-20 01:48:39

标签: javascript firebase google-cloud-firestore google-cloud-functions

我收到此错误:

ReferenceError: userid is not defined
  at exports.onUserNickUpdate.functions.firestore.document.onWrite (/srv/index.js:49:34)
    at cloudFunctionNewSignature (/srv/node_modules/firebase-functions/lib/cloud-functions.js:120:23)
    at /worker/worker.js:825:24
    at <anonymous>
    at process._tickDomainCallback (internal/process/next_tick.js:229:7)

当我运行此功能时(通过在文档上写入触发):

exports.onUserNickUpdate = functions.firestore.document('user/{userid}').onWrite((change,context) => {

    const changednick = change.after.data().nick

    return highscore_collection.doc(userid).set({
        nick: changednick
    })
})

错误来自此行highscore_collection.doc(userid).set。我不明白这个错误,为什么没有定义userid?

1 个答案:

答案 0 :(得分:1)

您从未定义变量userid。它不会自动创建,只是因为您在触发器定义中将其声明为通配符。请阅读using wildcards in a Firestore trigger上的文档。要获取其匹配值,请将其从提供给函数的上下文中拉出:

const userid = context.params.userid;