试图将值保存到自定义类型的数组中。 get'无法设置未定义的属性(设置' id;)"
我创建了一个这样的模型:
export class SelectedApplicationFeatures {
id: number;
}
并在我的ts文件中使用了它,如:
import { SelectedApplicationFeatures } from "src/app/models/selectedApplicationFeatures.model";
selectedApplicationFeatures: SelectedApplicationFeatures[]=[];
如果我尝试在这样的函数中设置selectedapplicationfeatures中的值:
for (let i = 0; i < _applicationFeatures.length; i++) {
if (_applicationFeatures[i].featureChecked == true) {
let j = 0;
this.selectedApplicationFeatures[j].id = _applicationFeatures[i].id;
//error is happening on the above line. _applicationFeatures[i].id is returning proper integer value.
j++;
}
}
我遇到了一个错误: “错误类型:无法读取未定义的属性(读取'0')”。
// _ applicationFeatures:
I had created a model like this:
export class SelectedApplicationFeatures {
id: number;
}
and used it in my ts file as:
import { SelectedApplicationFeatures } from "src/app/models/selectedApplicationFeatures.model";
selectedApplicationFeatures: SelectedApplicationFeatures[]=[];
then if I try to set values in selectedApplicationFeatures in my function like this:
for (let i = 0; i < _applicationFeatures.length; i++) {
if (_applicationFeatures[i].featureChecked == true) {
let j = 0;
this.selectedApplicationFeatures[j].id = _applicationFeatures[i].id;
//error is happening on the above line. _applicationFeatures[i].id is returning proper integer value.
j++;
}
}
I am getting an error:
"ERROR TypeError: Cannot read properties of undefined (reading '0')".
//_applicationFeatures:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该错误在于变量
selectionApplicationFeatures:SelecteApplicationFeatures [];
。您应该首先将其初始化为:然后,而不是将未知数的元素分配给
selecteApplicationFeatures
也许附加会更好。查看以下修复程序:The error lies in the variable
selectedApplicationFeatures: SelectedApplicationFeatures[];
. You should initialize it first as:And then, instead of assigning an unknown number of elements to
selectedApplicationFeatures
maybe appending would be better. Take a look at the fix below: