Javascript Don';如果文档已存在,则不希望覆盖该文档

Javascript Don';如果文档已存在,则不希望覆盖该文档,javascript,firebase,google-cloud-firestore,Javascript,Firebase,Google Cloud Firestore,这将覆盖它 firebase.firestore().collection('test_collection').doc(this.id).set({ body: this.body, }, { merge: true }) {merge:true}未更改它是否存在。{merge:true}用于合并两个对象,但会覆盖道具 您必须手动检查该单据是否存在: firebase.firestore().collection('test_collection') .doc(this.id)

这将覆盖它

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

{merge:true}未更改它是否存在。

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

您必须手动检查该单据是否存在:

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

在VueJS中,您可以创建这样的方法来检查文档的存在性,并在需要时通过作为道具传递在多个组件中使用它

methods: {
    async checkExistence (collectionId, docId) {
        const docSnapshot = await admin.firestore().collection(collectionId).doc(docId).get()
        return docSnapshot.exists
    },
    async updateDocument (collectionId, docId, data) {
        // Check if doc exists
        if (await checkExistence(collectionId, docId)) {
          //exists, update 
        } else { ... }
    }
}