使用Firestore中的Angular删除集合中的文档

使用Firestore中的Angular删除集合中的文档,angular,typescript,firebase,angularfire2,google-cloud-firestore,Angular,Typescript,Firebase,Angularfire2,Google Cloud Firestore,我正在尝试使用Angular删除Firebase中Firestore中文档集合中的文档及其嵌套文档。到目前为止,我可以删除上述文档和集合,但编译器会向我抛出一个错误错误TS2339:类型“{}”上不存在属性“id”。错误代码可能是对的,也可能是错的,我真的不知道,因为我的代码确实找到了与我正在查看的对象关联的id。这是我的代码: testID: string; private testDoc: AngularFirestoreDocument<Test>; constructor(

我正在尝试使用Angular删除Firebase中Firestore中文档集合中的文档及其嵌套文档。到目前为止,我可以删除上述文档和集合,但编译器会向我抛出一个错误
错误TS2339:类型“{}”上不存在属性“id”
。错误代码可能是对的,也可能是错的,我真的不知道,因为我的代码确实找到了与我正在查看的对象关联的id。这是我的代码:

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));
    }

我发现我正在查看的变量缺少类型,但是我不知道如何在arrow函数中声明该变量的类型,因为尝试将外部变量包含到函数中对我不起作用

我通过添加代码中的类型修复了错误。现在代码的工作原理与以前一样,它不会给我一个编译器错误

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.`));
}
我感谢你们中的一些人试图给我的帮助,但你们似乎更关注我的代码不起作用这一事实,而这从来都不是问题所在。重点是我在编译器抛出的问题中指定的错误,不管函数如何工作,以及如何正确编写代码以避免抛出错误


快速提醒,我不建议使用上述功能,因为该功能当前正在运行,同时订阅了问题集合,因此,当我在功能期间删除文档时,随着值的变化,代码也会运行多次。它可以工作,但没有经过优化。

您的
问题.id
是否有id字段,或者您需要密钥?它有字段id,我相信是的。我已经更新了问题以包含我正在查看的内容。这部分代码在做什么
this.testDoc.delete()
?测试是主文档,而问题是嵌套在主文档下的集合中的子文档。删除带有嵌套集合的文档是问题所在。但是,删除文档不会删除嵌套的集合。看起来您只是声明了它,没有为它分配任何内容。那么删除是如何工作的呢?
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.`));
}