【问题标题】:undefined is not iterable (cannot read property Symbol(Symbol.iterator))undefined 不可迭代(无法读取属性 Symbol(Symbol.iterator))
【发布时间】:2019-10-09 14:03:10
【问题描述】:

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

"ERROR TypeError: undefined is not iterable (cannot read property Symbol(Symbol.iterator))”。见这张图 =>

forum-admin-list.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();
    }
}

【问题讨论】:

  • 您在 subscribe() 之外循环遍历 fileList,这意味着它实际上不会等待 Observable 被解析。尝试在您的 subscribe() 中循环。此外,您可能想标记订阅结果as Files[] 而不是as File

标签: node.js angular typescript firebase


【解决方案1】:

您在 subscribe() 之外循环访问 fileList,这意味着它实际上不会等待 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

【讨论】:

  • 它说“i”没有定义。
  • 你能给this.fileList赋值后检查它是否未定义?也许您的 subscribe() 响应为空,或者您在 map() 中的逻辑有问题,导致列表未定义
猜你喜欢
  • 2021-10-09
  • 2021-02-20
  • 2021-04-28
  • 2017-11-03
  • 1970-01-01
  • 1970-01-01
  • 2022-06-14
  • 2020-11-30
  • 2019-08-05
相关资源
最近更新 更多