繁体   English   中英

在 Firestore 中更新后返回新更新的文档

[英]Return newly updated document after update in Firestore

我目前在 React 应用程序的前端初始化了 firebase。 在应用程序中,我使用以下代码更新用户文档:

  const handleProfileUpdate = async () => {
    const sourceRef = firestore()
      .collection('users')
      .where('userId', '==', state.currentUser.userId)
    sourceRef
      .get()
      .then(async snapshot => {
        if (snapshot.empty) {
          console.log('user not found')
        } else {
          snapshot.forEach(async doc => {
            await doc.ref.update({ firstName: detailsToUpdate })
            console.log(doc)
          })
        }
      })
      .catch(error => console.log(error.code))
  }

文档已成功更新,但我想返回对更新文档的引用,而不是再次查询用户文档。 .update() 是否返回对更新文档的引用,还是有其他方法可以解决这个问题?

Firestore 无法从写入方法(添加、设置、更新、删除)的结果中获取文档的更新值。 要获取新值,您必须调用doc.ref.get() ,从而对 firestore 服务器进行新读取。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM