简体   繁体   中英

Firebase Cloud Functions error_: variable is not defined

I get this error:

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)

When I run this function(gets triggered by write on document):

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

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

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

Error is from this line highscore_collection.doc(userid).set . I don't understand this error, how come userid is not defined?

You never defined the variable userid . It doesn't get automatically created just because you declared it as a wildcard in your trigger definition. Please read the documentation on using wildcards in a Firestore trigger . To get its matched value, pull it out of the context provided to the function:

const userid = context.params.userid;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM