Firebase 云功能未设置新文档

时间:2020-12-29 12:11:56

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

当用户在我的应用中注册时,有一个 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 后的其他日志:

enter image description here

1 个答案:

答案 0 :(得分:1)

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

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

现在,我确实在 users 集合中看到了新的经过验证的用户,一切都很好。