簡體   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