【问题标题】:Firebase Cloud Function not setting new documentFirebase Cloud Function 未设置新文档
【发布时间】:2021-04-06 01:40:05
【问题描述】:

当用户在我的应用中注册时,会有一个 Firebase 云功能,它将 authUidemailAddress 存储在 temporaryUsers 集合中,直到用户验证他的电子邮件地址。验证后,云函数会在users集合中创建一个新文档,并在temporaryUsers集合中删除用户的临时文档。

一切正常(临时文档在验证后被正确删除)除了在users 集合中创建的用户文档。由于某种原因,该文档没有被创建。

也许有人有关于如何更好地在此云功能中进行错误检查的提示?或者,有人发现我的代码中有任何问题/错误吗?这是云功能(下面有一些日志详细信息):

confirmEmail.js

import * as functions from 'firebase-functions'
import * as admin from 'firebase-admin'

export const confirmEmail = functions.https.onRequest(async (req, res) => {
  const confirmationHash = req.query.conf
  const auth = admin.auth()
  const store = admin.firestore()

  const querySnapshot = await store
    .collection('temporaryUsers')
    .where('confirmationHash', '==', confirmationHash)
    .get()

  // redirect user to email confirmation failure route if failure
  if (querySnapshot.size === 0) {
    return res.redirect('http://localhost:3000/email-confirmation/failure')
  }

  // else get the temporaryuser doc
  const temporaryUserDoc = querySnapshot.docs[0]
  // grab properties we want from the temporary user doc
  const { authUid, emailAddress, displayName } = temporaryUserDoc.data()

  // update our user's Firebase Auth account and set the email verified property to true
  await auth.updateUser(authUid, { emailVerified: true })
  // add verified user to users collection
  try {
    const docRef = await store.collection('users').doc(authUid).set({
      emailAddress,
      displayName
    })
    console.log('doc updated!', docRef)
  } catch (error) {
    console.log('error in updating user', error)
  }

  // after user verified, delete temporary user doc 
  await store.collection('temporaryUsers').doc(temporaryUserDoc.id).delete()
  // redirect user to success page after successful verification
  // return res.redirect('http://localhost:3000/email-confirmation/success')
  // TO DO: redirect user to success page and then have them login again
  return res.redirect('http://localhost:3000/')
})

日志:


5:53:42.241 AM
confirmEmail
Function execution took 4824 ms, finished with status code: 302
5:53:37.418 AM
confirmEmail
Function execution started
5:52:49.984 AM
sendVerificationEmail
Function execution took 3400 ms, finished with status: 'ok'
5:52:49.981 AM
sendVerificationEmail
it works
5:52:46.585 AM
sendVerificationEmail
Function execution started
5:52:45.389 AM
createAccount
Function execution took 4568 ms, finished with status code: 200
5:52:43.796 AM
createAccount
}
5:52:43.796 AM
createAccount
createdAt: 1609242763793
5:52:43.796 AM
createAccount
confirmationHash: '38563d53-be8e-4402-8f21-fb0260e3d7d4',
5:52:43.796 AM
createAccount
emailAddress: 'redacted@gmail.com',
5:52:43.796 AM
createAccount
displayName: 'doss',
5:52:43.796 AM
createAccount
authUid: 'EMUxT8qYz4SJJqKILHt0QU70iZy2',
5:52:43.796 AM
createAccount
tempUserInfo from createTemporaryUserJs: {
5:52:40.822 AM
createAccount
Function execution started
5:52:40.747 AM
createAccount
Function execution took 13 ms, finished with status code: 204
5:52:40.735 AM
createAccount
Function execution started

将 try/catch 添加到 confirmEmail.js 后的其他日志:

【问题讨论】:

  • “也许有人有关于如何更好地在此云功能中进行错误检查的提示” => 使用 try/catchconsole.log()。乍一看,我在您的代码中看不到任何问题。您确定authUid 的值正确吗?
  • 让我调查一下然后回来。谢谢!
  • @RenaudTarnec:我添加了一个 try/catch(参见上面编辑过的代码),我现在在日志中看到的唯一内容是:doc updated! WriteResult { _writeTime: Timestamp { _seconds: 1609255658, _nanoseconds: 835622000 } 。也没有新的用户文档添加到users 集合中。
  • 我想通了。这是我需要从临时文件夹转移到用户文件夹的缺失参数。将用答案更新我的问题。感谢您的帮助!

标签: node.js google-cloud-firestore firebase-authentication google-cloud-functions


【解决方案1】:

我想通了。这是我的一个错误。我正在查看 firebase firestore 控制台并通过 createdAt 属性过滤数据....但新数据没有显示,因为我的 confirmEmail.js 函数没有添加 createdAt 属性:

    const docRef = await store.collection('users').doc(authUid).set({
      emailAddress,
      displayName,
      createdAt <---- i forgot this 
    })

现在确实如此,我确实在 users 集合中看到了新的已验证用户,并且一切都很好。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-02
    • 2021-10-28
    • 2021-03-24
    • 1970-01-01
    • 2021-11-23
    • 2020-07-27
    相关资源
    最近更新 更多