Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/446.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何确保所有数据按顺序写入Firestore?_Javascript_Reactjs_Google Cloud Firestore - Fatal编程技术网

Javascript 如何确保所有数据按顺序写入Firestore?

Javascript 如何确保所有数据按顺序写入Firestore?,javascript,reactjs,google-cloud-firestore,Javascript,Reactjs,Google Cloud Firestore,我正在尝试创建一个用户配置文件,声明该配置文件来自我应用程序中的一个企业主。它应该创建概要文件,然后将“角色”数组等信息与其中的“businessOwner”合并,并添加“businessId” 有时,代码可以无缝地工作。在其他时候,只有角色和businessId会传递给创建的用户(而所有其他信息都不会!) 这是createUserWithEmailAndPassword: async createUserProfileDocument(user, additionalData) { i

我正在尝试创建一个用户配置文件,声明该配置文件来自我应用程序中的一个企业主。它应该创建概要文件,然后将“角色”数组等信息与其中的“businessOwner”合并,并添加“businessId”

有时,代码可以无缝地工作。在其他时候,只有角色和businessId会传递给创建的用户(而所有其他信息都不会!)

这是createUserWithEmailAndPassword:

async createUserProfileDocument(user, additionalData) {
    if (!user) return
    const userRef = this.firestore.doc(`users/${user.uid}`)
    const snapshot = await userRef.get()
    if (!snapshot.exists) {
      const { displayName, email, photoURL, providerData } = user
      try {
        await userRef.set({
          displayName,
          email,
          photoURL, 
          ...additionalData,
          providerData: providerData[0].providerId,
        })
      } catch (error) {
        console.error('error creating user: ', error)
      }
    }
    return this.getUserDocument(user.uid)
  }

我认为问题出在这一行
const snapshot=await userRef.get()


如中所述,您应该使用
then()
函数获取快照,以便首先返回承诺。

我认为您还需要等待以下消息:-

await userService.createUserProfileDocument(values.user)
由于您正在此处设置用户信息(await userRef.set),如果您不等待承诺,那么有时,您的下一个代码块(await userRef.set({roles:['businessOwner',)会执行,然后您的承诺可能会得到解决。因此,有时您可能无法获得其他信息


您还需要处理createUserProfileDocument的错误情况。

我认为问题出在这一行
const snapshot=wait userRef.get()
。您必须使用
then()
为了确保您在承诺返回后获得成功。请检查如何获取文档。这样我就可以在
createUserProfileDocument
函数中完全摆脱async Wait?我的评论是,为了正确解决承诺,您需要使用
then()
以确保在承诺返回后获取快照。
await userService.createUserProfileDocument(values.user)