简体   繁体   English

删除文档Vuejs Firestore的问题

[英]problem deleting document Vuejs Firestore

I am creating site for my store and learning Vue js at the same time.我正在为我的商店创建网站并同时学习 Vue js。 I'm having a problem with delete product using id.我在使用 id 删除产品时遇到问题。 I'm using Vue js 3 and Firebase 9.我正在使用 Vue js 3 和 Firebase 9。

I have this on main.j我在 main.j 上有这个

const dataBase = collection(db, "products");

and this on products.js这在 products.js

import { dataBase } from '../main';
import { addDoc, deleteDoc, onSnapshot, doc } from "firebase/firestore";

export default {
  name: "Products",
  props: {
    msg: String
  },
  data() {
    return {
      products: [],
      product: {
        name: '',,
        price: '',
        brand: '',
        category: ''
      }
    }
  },
  methods: {
    saveData() {
      try {
        addDoc(dataBase, this.product).then((docRef) => {
          console.log("Document written with ID: ", docRef.id);
        })
      } catch (e) {
        console.error("Error adding document: ", e);
      }
    },
    deleteProduct(doc) {
      if (confirm('Видалити ?')) {
        deleteDoc(doc(dataBase, "products", docRef.id));
      } else {

      }
    }
  },
  created() {
    onSnapshot(dataBase, (snapshot) => {
      snapshot.docs.forEach((doc) => {
        this.products.push({ ...doc.data(), id: doc.id })
      })
    });
  }
};

Thanks!谢谢!

I think it's a typo issue, instead of docRef.id you need right doc.id我认为这是一个错字问题,而不是docRef.id你需要正确的doc.id

deleteProduct(doc) {
  if (confirm('Видалити ?')) {
    deleteDoc(doc(dataBase, "products", doc.id));
  } else {

  }
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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