firebase - Firestore云函数: cannot read property 'userId' of undefined

标签 firebase google-cloud-platform google-cloud-firestore google-cloud-functions cloud

我无法从云功能发送欢迎电子邮件。每当在我的 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: '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="03607a61667177716c6d69603043646e626a6f2d606c6e" rel="noreferrer noopener nofollow">[email protected]</a>',
            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)

最佳答案

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

exports.welcomeEmail = functions.firestore.document('Users/{userId}')
.onCreate((snap, context)=>{
  const user = snap.data();
  const msg = {
        to: user.email,
        from: '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="086b716a6d7a7c7a6766626b3b486f65696164266b6765" rel="noreferrer noopener nofollow">[email protected]</a>',
        subject: 'Welcome to COFOZ',
        templateId: '1c455865-4529-4ae1-8e5a-9a5b8eaf0157',
        substitutionsWrappers: ['{{', '}}'],
        substitutions: {
            name: user.name
        }
  return sgMail.send(msg)
}

关于firebase - Firestore云函数: cannot read property 'userId' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50250719/

相关文章:

firebase - 从Firebase DB中的 'map'读入Flutter中的ListView

Android Firebase 数据库加载更快

java - 为什么我会收到此错误 "FirebaseRecyclerAdapter() in FirebaseRecyclerAdapter cannot be applied to:"

node.js - TypeError : db. collection(...).document 不是 firestore 中的函数

python - 我是否需要清理/删除在部署我的 Cloud Run 实例时创建的图像?

node.js - 如何在 Cloud Run 中获取环境变量?

java - Firestore DB 模型、文档或子集合中的属性

android - com.firebase.client.FirebaseException : Failed to parse node with class class CLASS_NAME android

javascript - 用js构建声誉/业力系统

python - 获取 Google 群组成员