Reputation: 87
I'm having an issue getting all the documents from a collection. I'm essentially trying to update a subcollection by deleting it and then recreating it with the new data. Here is my function:
async deleteIngredientSubCollection(recipeId: string) {
const collectionReference = collection(
this.db,
'recipe',
recipeId,
'ingredients'
);
let querySnapshot = await getDocs(collectionReference);
querySnapshot.docs.forEach(async (ingredient) => {
let i = ingredient.data() as Ingredient;
let ingredientToDeleteReference = doc(
this.db,
'recipe',
recipeId,
'ingredients',
i.id
);
await deleteDoc(ingredientToDeleteReference);
});
}
The querySnapshot returns no data which I'm assuming is because I am not passing in a query but a collectionReference only.
I have also tried the following:
const snapshot = this.db.collection('ingredients').get();
snapshot.forEach((doc) => {
console.log(doc.id, '=>', doc.data());
});
And the error I recieve is: Property 'collection' does not exist on type 'Firestore.'
. Any assistance will be appreciated.
Upvotes: 0
Views: 1498
Reputation: 379
As @Frank Van Puffelen mentioned, Error Property 'collection' does not exist on type 'Firestore.'
can be caused due to the querySnapshot received is empty, it probably means there are really no documents at that path. We can check this using console.log("'"+recipeId+"'")
to see if it matches an actual collection in the database.
Refer to document Firebase Document which mentions, If there is no document at the location referenced by docRef, the resulting document will be empty and calling exists on it will return false.
@Damien Garlinge updated, this issue is resolved, by correcting the collection name from recipe to recipes.
Upvotes: 1