【问题标题】:Firebase Cloud Functions error_: variable is not definedFirebase Cloud Functions 错误_:未定义变量
【发布时间】:2019-05-20 01:48:38
【问题描述】:

我收到此错误:

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?

【问题讨论】:

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


    【解决方案1】:

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

    const userid = context.params.userid;
    

    【讨论】:

      猜你喜欢
      • 2018-06-11
      • 1970-01-01
      • 1970-01-01
      • 2018-06-17
      • 2017-09-14
      • 2019-02-04
      • 2018-10-02
      • 2020-11-30
      • 2018-08-10
      相关资源
      最近更新 更多