Firebase Firestore云函数:无法读取属性';用户ID';未定义的

Firebase Firestore云函数:无法读取属性';用户ID';未定义的,firebase,cloud,google-cloud-firestore,google-cloud-functions,Firebase,Cloud,Google Cloud Firestore,Google Cloud Functions,我无法从云功能发送欢迎电子邮件。每当在我的firestore中使用路径为Users/userId的“Users”集合创建新用户时 exports.welcomeEmail = functions.firestore.document('Users/{userId}') .onCreate((snap, context)=>{ const userId = context.params.userId; const db = admin.firestore()

我无法从云功能发送欢迎电子邮件。每当在我的firestore中使用路径为Users/userId的“Users”集合创建新用户时

        exports.welcomeEmail = functions.firestore.document('Users/{userId}')
.onCreate((snap, context)=>{
    const userId = context.params.userId;

    const db = admin.firestore()

    return db.collection('Users').doc(userId)

    .get()
    .then(doc => {

        const user = doc.data()

        const msg = {
            to: user.email,
            from: 'cybertronjc3@gmail.com',
            subject: 'Welcome to COFOZ',

            templateId: '1c455865-4529-4ae1-8e5a-9a5b8eaf0157',
            substitutionsWrappers: ['{{', '}}'],
            substitutions: {
                name: user.name
            }
        };

        return sgMail.send(msg)

    })
    .then(() => console.log('email sent!'))
    .catch(err => console.log(err))
})
这就是我得到的错误

TypeError: Cannot read property 'userId' of undefined
    at exports.welcomeEmail.functions.firestore.document.onCreate.event (/user_code/index.js:19:32)
    at Object.<anonymous> (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:112:27)
    at next (native)
    at /user_code/node_modules/firebase-functions/lib/cloud-functions.js:28:71
    at __awaiter (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:24:12)
    at cloudFunction (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:82:36)
    at /var/tmp/worker/worker.js:710:26
    at process._tickDomainCallback (internal/process/next_tick.js:135:7)
TypeError:无法读取未定义的属性“userId”
在exports.welcomeEmail.functions.firestore.document.onCreate.event(/user\u code/index.js:19:32)
反对。(/user_code/node_modules/firebase functions/lib/cloud functions.js:112:27)
在下一个(本地)
at/user\u code/node\u modules/firebase functions/lib/cloud functions.js:28:71
at_uuwaiter(/user_code/node_modules/firebase functions/lib/cloud functions.js:24:12)
在cloudFunction(/user\u code/node\u modules/firebase functions/lib/cloud functions.js:82:36)
at/var/tmp/worker/worker.js:710:26
在进程中。_tickDomainCallback(internal/process/next_tick.js:135:7)

您可能正在使用旧版本的云功能SDK。您可以检查“依赖项”节点下的
package.json
文件中使用的版本吗

根据代码中使用的语法,应该有一个等于或高于1.0.0的版本

有关更多信息,请参阅此文档(迁移指南):并检查package.json文件中的版本

您将看到,要更新到新SDK,您应该执行以下操作:

npm安装firebase-functions@latest--保存

npm安装firebase-admin@5.11.0--保存


无需通过在文档中查找userid来获取新用户。只需从snap获取新创建的文档

exports.welcomeEmail = functions.firestore.document('Users/{userId}')
.onCreate((snap, context)=>{
  const user = snap.data();
  const msg = {
        to: user.email,
        from: 'cybertronjc3@gmail.com',
        subject: 'Welcome to COFOZ',
        templateId: '1c455865-4529-4ae1-8e5a-9a5b8eaf0157',
        substitutionsWrappers: ['{{', '}}'],
        substitutions: {
            name: user.name
        }
  return sgMail.send(msg)
}