简体   繁体   中英

Return newly updated document after update in Firestore

I currently have firebase initialized on the front end of a react application. Within the application, I am updating the user document using the following code:

  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))
  }

The document is successfully updated, but I would like to return a reference to the updated document as opposed to having to query the user document again. Does .update() return a reference to the updated document or is there another way I should go about this?

Firestore doesn't have a way to get the updated values of a document from the result of a write method(add, set, update, delete). To get the new values, you have to call doc.ref.get() , thereby making a new read to the firestore servers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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