简体   繁体   English

Angular:类型错误:无法使用 Treeview 读取未定义的属性(读取“forEach”)

[英]Angular: TypeError: Cannot read properties of undefined (reading 'forEach') with Treeview

I try execute on my view-post.component.ts with treeview我尝试使用 treeview 在我的 view-post.component.ts上执行

在此处输入图像描述

Here is my code这是我的代码


  constructor(private postService: PostService, private activateRoute: ActivatedRoute,
    private commentService: CommentService, private router: Router) {
    this.postId = this.activateRoute.snapshot.params.id;

    this.commentForm = new FormGroup({
      text: new FormControl('', Validators.required)
    });
    this.commentPayload = {
      text: '',
      postId: this.postId
    };
    this.dataSource.data = this.comments;
  }
private _transformer = (node: CommentPayload, level: number) => {
  return {
    expandable: !!node.dtoList && node.dtoList.length > 0,
    username: node.username,
    text: node.text,
    level: level,
  };
};

treeControl = new FlatTreeControl<ExampleFlatNode>(
  (node) => node.level,
  (node) => node.expandable
);

treeFlattener = new MatTreeFlattener(
  this._transformer,
  (node) => node.level,
  (node) => node.expandable,
  (node) => node.dtoList
);

dataSource = new MatTreeFlatDataSource(this.treeControl, this.treeFlattener);


hasChild = (_: number, node: ExampleFlatNode) => node.expandable;
}

I only want show something in console for every item is on "treeview" but show me this error My code base on this project Stackblitz * https://stackblitz.com/edit/angular-rtzh5a?file=src%2Fapp%2Ftree-flat-overview-example.html我只想在控制台中为每个项目在“treeview”上显示一些内容,但向我显示此错误我的代码基于此项目 Stackblitz * https://stackblitz.com/edit/angular-rtzh5a?file=src%2Fapp%2Ftree-平面概述示例.html

Cannot read properties of undefined (reading "forEach")无法读取未定义的属性(读取“forEach”)

Meaning that there is a place in your code, where you are using foreach on some array, which is null or undefined.这意味着您的代码中有一个地方,您在某个数组上使用 foreach,它是 null 或未定义。 Provided code sample doesn't contain this part, but if you will run app with 'ng serve' (on localhost), this console error will provide you also a component name and on which line problem occoured.提供的代码示例不包含此部分,但如果您将使用“ng serve”(在本地主机上)运行应用程序,此控制台错误还将为您提供组件名称以及发生在哪一行的问题。 For now it's saying main.js:1, because it's built version.现在它说的是 main.js:1,因为它是构建版本。 You can easily secure it like this:您可以像这样轻松保护它:

if (something) {
  something.forEach(s => {
    // ...
  });
}

or use forof loops (like the example above, it just won't run when something is null/undefined)或者使用 forof 循环(就像上面的例子,当某些东西为 null/undefined 时它不会运行)

for (let s of something) {
  // ...
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TypeError:无法读取未定义的属性(读取“forEach”) - TypeError: Cannot read properties of undefined (reading 'forEach') TypeError:无法读取未定义的属性(读取&#39;forEach) - TypeError: Cannot read properties of undefined (reading 'forEach) TypeError: Cannot read properties of undefined (reading &#39;forEach&#39;) 如何解决这个问题? - TypeError: Cannot read properties of undefined (reading 'forEach') How to overcome this problem? files.forEach((file) => { TypeError: 无法读取未定义的属性(读取 'forEach') - files.forEach((file) => { TypeError: Cannot read properties of undefined (reading 'forEach') TypeError:无法读取未定义的属性(读取&#39;indexOf&#39;) - TypeError: Cannot read properties of undefined (reading 'indexOf') TypeError:无法读取未定义的属性(读取“命令”) - TypeError: Cannot read properties of undefined (reading 'commands') TypeError:无法读取未定义的属性(读取“最高”) - TypeError: Cannot read properties of undefined (reading ‘highest’) TypeError:无法读取未定义的属性(读取“findOne”) - TypeError: Cannot read properties of undefined (reading 'findOne') TypeError:无法读取未定义的属性(读取“发送”) - TypeError: Cannot read properties of undefined (reading 'send') TypeError:无法读取未定义的属性(读取“useParams”) - TypeError: Cannot read properties of undefined (reading 'useParams')
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM