首页 > 解决方案 > 如果文档已经存在,不想覆盖它

问题描述

这将覆盖它。

firebase.firestore().collection('test_collection').doc(this.id).set({
 body: this.body,
}, { merge: true })

{merge: true} 无论是否存在都没有改变。

标签: javascriptfirebasegoogle-cloud-firestore

解决方案


{merge: true} 用于合并两个对象,但会覆盖 props。

您必须手动检查文档是否存在:

firebase.firestore().collection('test_collection')
   .doc(this.id)
   .get()
   .then(docSnapshot => {
      if(!docSnapshot.exists)
         docSnapshot.ref.set({body: this.body})
   })

推荐阅读