I use this code below and run well in Firebase RealTime Database:
const onDelete = (id) => {
if (
window.confirm("Are you sure that you wanted to delete the contact ?")
) {
Db.child(`contacts/${id}`).remove((err) => {
if (err) {
alert(err);
} else {
alert("Contact Deleted Successfully");
console.timeStamp(id);
}
});
}
};
Now I create another database in Firebase Firestore but cannot delete.
const onDelete = (id) => {
if (window.confirm("Are you sure that you wanted to delete contact ?")) {
firebase
.firestore()
.collection("contacts")
.doc(`${id}`)
.delete()
.then(() => {
console.log("Document successfully deleted!");
})
.catch((error) => {
console.error("Error removing document: ", error);
});
}
};
CodePudding user response:
There's no difference in the databases here: in both cases you have to specify the full path to the data to be deleted.
If you can somehow specify line numbers for the Realtime Database, most likely you've used those line numbers as keys in your database. And if you can't do the same on Firestore, it's likely you didn't use the line numbers as document IDs there. If that is indeed the case, you'll need to either maintain a mapping from line numbers to document IDs, or use the line numbers for the document IDs in Firestore too.
CodePudding user response:
const onDelete = async (id) => { if ( window.confirm("Are you sure that you wanted to delete the contact ?") ) { firebase .firestore() .collection("contacts") .get() .then(async (querySnapshot) => { await deleteDoc(doc(db, "contacts", querySnapshot.docs[id].id)); alert("Contact Deleted Successfully"); }); } };