I am getting the error
Uncaught (in promise) FirebaseError: Invalid collection reference. Collection references must have an odd number of segments, but
Accounts/sKnjAQzJ1sQ5qRC4ki6b/RequestsReceived/m27vTB22izq6UixWP2s6 has 4.
My code is
var ref2 = collection(db, "Accounts/" localStorage.getItem('Id') "/RequestsReceived", id);
const docRef2 = await getDoc(ref2);
await deleteDoc (ref2);
The document I want to delete has the URL /Accounts/sKnjAQzJ1sQ5qRC4ki6b/RequestsReceived/m27vTB22izq6UixWP2s6
I understand that if you add a document, it has to have an odd number of path, the collection name, or collection/document/collection. But shouldn't deleteDoc have an even amount because I'm deleting collection/document or collection/document/collection/document. What am I doing wrong?
CodePudding user response:
"Accounts/" localStorage.getItem('Id') "/RequestsReceived", id
This path is pointing towards a document in 'RequestsReceived' sub-collection. So you should be using doc()
to create a DocumentReference.
Also you don't need to fetch a document before deleting as long as you have the reference:
// ensure all values are defined
console.log(localStorage.getItem('Id'), id);
// use doc()
var ref2 = doc(db, "Accounts/" localStorage.getItem('Id') "/RequestsReceived", id);
await deleteDoc(ref2);