Javascript 在Firestore中更新后返回新更新的文档
我目前在react应用程序的前端初始化了firebase。在应用程序中,我使用以下代码更新用户文档:Javascript 在Firestore中更新后返回新更新的文档,javascript,reactjs,firebase,google-cloud-firestore,Javascript,Reactjs,Firebase,Google Cloud Firestore,我目前在react应用程序的前端初始化了firebase。在应用程序中,我使用以下代码更新用户文档: const handleProfileUpdate = async () => { const sourceRef = firestore() .collection('users') .where('userId', '==', state.currentUser.userId) sourceRef .get() .th
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服务器进行新的读取。您在doc.ref
中已经有了该引用。啊,好的,我不确定是否可以获取该文档的引用。谢谢