gpt4 book ai didi

javascript - 无法读取未定义的属性 'push',试图将信息推送到对象中

转载 作者:行者123 更新时间:2023-11-30 14:01:19 25 4
gpt4 key购买 nike

I am trying to add information to an object but I am getting an error (saying that property 'push' of undefined).

/*This is my object model:*/

export class Students {
city: string;
company: string;
email: string;
firstName: string;
grades: [];
id: string;
lastName: string;
pic: string;
skill: string;
open: boolean;
tags: string[];
}

filteredStudents: 学生[];

/*I have a form that gets the information user is typing.
f has the information and index_ has the index of the object.*/

sendForm(f: NgForm, index_: number) {

    if (f.invalid) {
return
}
else {
/*storing the value of user input into "str variable"*/
var str = f.value.inputTag;

/*Here is where the error happens. I am trying to store information into "tags" property of a specific student.*/
this.filteredStudents[index_].tags.push(str);
}
f.reset();
}

如果你知道发生了什么,请帮助我!如果您知道发生了什么,请帮助我!如果您知道发生了什么,请帮助我!

此错误出现在 google chrome 的控制台中。

最佳答案

我认为这是因为 this.filteredStudents 未在任何地方定义,因此您需要将其声明为带有键“标签”的散列,并将值声明为 Controller 顶部某处的列表。

this.filteredStudents = {
tags: []
}

那应该让你做this.filteredStudents[tags].push('whatever').

索引末尾有 _ 也有点奇怪。我已经看到内部函数的名称以 w/_ 开头,但没有看到它在函数参数的末尾使用。

关于javascript - 无法读取未定义的属性 'push',试图将信息推送到对象中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56278596/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com