从 Firestore 集合中删除所有文档

To delete all documents from a firestore collection

我正在尝试使用这个方法:

deleteMessages(){
    this.firestore.collection("MESSAGES")
  .get()
  .then(res => {res.forEach(element => {element.ref.delete();});

      });
     }

但我收到以下错误消息:

Property 'then' does not exist on type 'Observable<QuerySnapshot>'.

然后我选择了以下:

deleteTheMessages() {
      const messagesCollection= this.firestore.collection<Message>('MESSAGES').get();
  
      messagesCollection.toPromise().then((snapshot) => {
        snapshot.forEach((doc) => doc.ref.delete());
      });
    }

toPromise was struck :

然后我在尝试 ng 构建时收到了这条消息

error: src/app/messages.service.ts:37:9 - error TS2532: Object is possibly 'undefined'.

37 snapshot.forEach((doc) => doc.ref.delete());

用~~~~~~~~ snapshot 一词下。

无法同时解决,所以如果您有任何建议,我将不胜感激。

解决方案是按照@Doug Stevenson 的建议添加 check-in:

if(snapshot)