undefined是不可迭代的(无法读取属性Symbol(Symbol.iterator))

时间:2019-05-23 03:31:43

标签: node.js angular typescript firebase

我试图遍历fileList以便执行删除查询。首先,我从数据库中输入属性"postnumber"=user的表“文件”中获取数据。然后将其保存到“ fileList:Files[]”中。然后循环遍历此fileList以便执行删除查询。但它一直说

  

“ ERROR TypeError:未定义是不可迭代的(无法读取属性   Symbol(Symbol.iterator))”。参见此图片=>

error.jpg

论坛管理列表.component.ts

import { FileService } from 'src/app/shared/file.service';
import { Files } from 'src/app/shared/files.model';
export class ForumAdminListComponent {
fileList:Files[];
onDelete(pNo:string){ 
        this.fservice.getPost(pNo).subscribe(actionArray => {
          this.fileList = actionArray.map(item => {
            return {
              id: item.payload.doc.id,
              ...item.payload.doc.data()
            } as Files;
          })
        });
        for(let i of this.fileList){
          this.storage.storage.refFromURL(i.path).delete();
          this.firestore.doc("files/"+i.id).delete();
        }
  }
}

files.model.ts

export class Files {
    id:string;
    pNo:string;
    downloadURL:string;
    path:string;
}

file.service.ts

export class FileService {
    formData: Files; 
    constructor(private firestore: AngularFirestore) { }

    getPost(userRef){
      return this.firestore.collection('files',ref=>ref.where('pNo','==',userRef)).snapshotChanges();
    }
}

1 个答案:

答案 0 :(得分:0)

您正在fileList之外遍历subscribe(),这意味着它实际上不会等待Observable解析。尝试在subscribe()内部循环。

onDelete(pNo:string){ 
  this.fservice.getPost(pNo).subscribe(actionArray => {
    this.fileList = actionArray.map(item => {
      return {
        id: item.payload.doc.id,
        ...item.payload.doc.data()
      } as Files[];

      for(let i of this.fileList){
        this.storage.storage.refFromURL(i.path).delete();
        this.firestore.doc("files/"+i.id).delete();
      }
    })
  });
}

此外,您可能希望标记订阅as Files[]而不是as File的结果