Bruhno
Bruhno

Reputation: 61

Deleting docs in a collection using Angular in Firestore

I am trying to delete a document and its nested documents in a collection inside the document in Firestore in Firebase using Angular. As of right now, I can delete said document and collection, but not without the compiler throwing an error at me error TS2339: Property 'id' does not exist on type '{}'. The error code might be right or wrong, I do honestly not know, as my code does find the id associated with the object I'm looking at. This is my code:

testID: string;
private testDoc: AngularFirestoreDocument<Test>;

constructor(private db: AngularFirestore) { }

deleteDoc() {
    console.log('Deleting test');
    this.db.collection(`tests/${this.testID}/questions`, ref => ref.orderBy('order'))
        .valueChanges().subscribe(questions => {
            questions.map(question => {
                this.db.doc(`tests/${this.testID}/questions/${question.id}`).delete()
                    .catch(error => {console.log(error); })
                    .then(() => console.log(`tests/${this.testID}/questions/${question.id}`));
            });
        });
        this.testDoc.delete().catch(error => console.log(error));
    }

I have tried to pass 2 arguments in the map function as well but that just throws errors in the executed function instead of the compiler, thus making the code not work at all (I might not be passing the right thing here).

As I've mentioned, my code works, but I am most definitely doing something wrong. So my question is, what am I doing wrong to trigger a compile error and how do I avoid this while making sure the function still works? Hope someone can help clarify what it is I am doing wrong here.

Thank you in advance.

*Edit, included the doc I'm looking at

export interface Question {
    description: string;
    order: number;
    type: string;
    id: string;
    name: string;
    options?: Object[];
    radio_amount?: number;
    current_chips?: Object[];
    imageURL?: string;
}

Upvotes: 4

Views: 11085

Answers (1)

Bruhno
Bruhno

Reputation: 61

I figured that I was missing the type on the variable I was looking at, I did however not know how to declare the type of that variable in a arrow function as trying to include external variables into the function did not work for me.

I fixed my error by adding the type as seen in my code here. Now the code works just like before AND it does not give me a compiler error.

deleteDoc() {
    this.db.collection(`tests/${this.testID}/questions`,
        ref => ref.orderBy('order')).valueChanges().subscribe(questions => {
        questions.map((question: Question) => {
            this.db.doc(`tests/${this.testID}/questions/${question.id}`).delete()
                .catch(error => {console.log(error); })
                .then(() => console.log(`Deleting question (${question.id}) in (${this.testID})`));
        });
    });
    this.testDoc.delete().catch(error => console.log(error)).then(() => console.log(`${this.testID} has been deleted.`));
}

I appreciate the help a few of you have tried to give me, but you seemed to be more focused on the fact that my code did not work when that was never the case or the question. The focus was on the error I specified in the question thrown by the compiler despite the function working and how to write the code correctly to not throw the error.

Quick heads up, I do not recommend using the above mentioned function since this is currently running while subscribed to the collection of questions and is thus also running the code several more times as the value changes when I delete the docs during the function. It works, but it is not optimized.

Upvotes: 2

Related Questions