Чтобы удалить документ в Firestore с помощью JavaScript, вы можете использовать несколько методов. Вот некоторые из наиболее распространенных подходов:
-
Использование метода
delete():const docRef = db.collection("yourCollection").doc("yourDocument"); docRef.delete() .then(() => { console.log("Document successfully deleted!"); }) .catch((error) => { console.error("Error removing document: ", error); }); -
Использование метода
FieldValue.delete():const docRef = db.collection("yourCollection").doc("yourDocument"); docRef.update({ yourField: firebase.firestore.FieldValue.delete() }) .then(() => { console.log("Field successfully deleted from the document!"); }) .catch((error) => { console.error("Error removing field from the document: ", error); }); -
Использование транзакции:
const docRef = db.collection("yourCollection").doc("yourDocument"); db.runTransaction((transaction) => { return transaction.get(docRef).then((doc) => { if (doc.exists) { transaction.delete(docRef); console.log("Document successfully deleted!"); } else { console.log("Document does not exist."); } }); }) .catch((error) => { console.error("Error deleting document: ", error); });
Эти методы позволяют удалить документ из Firestore с помощью JavaScript. Не забудьте заменить «ваша коллекция» фактическим названием вашей коллекции, а «ваш документ» — идентификатором или ссылкой на документ, который вы хотите удалить.