Javascript 角度2-对象属性返回为未定义,但它们存在
我有这个问题。我正在使用Angular并调用API来获取一个包含其他对象的对象。问题是我无法访问这些对象,因为它们返回时未定义。然而,将它们记录在控制台中(使用Google Chrome),对象实际上就在那里 以下是我的代码的相关片段: 调查问卷.组件.tsJavascript 角度2-对象属性返回为未定义,但它们存在,javascript,angular,typescript,Javascript,Angular,Typescript,我有这个问题。我正在使用Angular并调用API来获取一个包含其他对象的对象。问题是我无法访问这些对象,因为它们返回时未定义。然而,将它们记录在控制台中(使用Google Chrome),对象实际上就在那里 以下是我的代码的相关片段: 调查问卷.组件.ts export class QuestionnaireComponent extends BaseComponent { questionnaires: Models.Survey.Rootobject; ... n
export class QuestionnaireComponent extends BaseComponent {
questionnaires: Models.Survey.Rootobject;
...
ngOnInit() {
...do stuff...
Observable.forkJoin(this.service.getAllQuestionnaires())
.subscribe(res => {
this.questionnaires = res[0];
console.log(this.questionnaires);
console.log(res[0]);
console.log(this.questionnaires.questionnaireItems);
this.numberOfQuestionnaires = this.questionnaires.questionnaireItems.length;
...do more stuff...
});
}
...
}
...code...
getAllQuestionnaires() {
return this.http.get<Models.Survey.Rootobject>('/api/v1/xmlquestionnaires').map(res => res[0]);
}
declare namespace Models.Survey {
...more interfaces...
export interface QuestionnaireItem {
id: string;
name: string;
order: string;
description: string[];
finalText: string[];
questionGroups: QuestionGroup[];
questions: Question[];
toRandomize: string;
}
export interface Rootobject {
questionnaireItems: QuestionnaireItem[];
}
export interface Wrapper {
rootobject: Rootobject;
}
}
问卷.服务.ts
export class QuestionnaireComponent extends BaseComponent {
questionnaires: Models.Survey.Rootobject;
...
ngOnInit() {
...do stuff...
Observable.forkJoin(this.service.getAllQuestionnaires())
.subscribe(res => {
this.questionnaires = res[0];
console.log(this.questionnaires);
console.log(res[0]);
console.log(this.questionnaires.questionnaireItems);
this.numberOfQuestionnaires = this.questionnaires.questionnaireItems.length;
...do more stuff...
});
}
...
}
...code...
getAllQuestionnaires() {
return this.http.get<Models.Survey.Rootobject>('/api/v1/xmlquestionnaires').map(res => res[0]);
}
declare namespace Models.Survey {
...more interfaces...
export interface QuestionnaireItem {
id: string;
name: string;
order: string;
description: string[];
finalText: string[];
questionGroups: QuestionGroup[];
questions: Question[];
toRandomize: string;
}
export interface Rootobject {
questionnaireItems: QuestionnaireItem[];
}
export interface Wrapper {
rootobject: Rootobject;
}
}
“console.log(x)”的结果如下:
QuestionnaireItems位于对象中,但直接访问它将返回undefined
你知道问题是什么吗?我该如何解决
多谢各位 其正常返回值未定义。JS很困惑,因为若你们像这个问题将要解决的那个样使用它,你们想访问数组中的数组 在你的例子中,我将索引1到3
console.log(this.questionnaries[i].questionnaireItems)
看起来您的方法
getAllQuestions()
返回的是包装器
对象,而不是Rootobject
在这种情况下,只需将“.rootobject”添加到方法中即可编辑:并修复类型,可能如下所示:
getAllQuestionnaires() {
return this.http
.get<Models.Survey.Wrapper>('/api/v1/xmlquestionnaires')
.map(res => res[0].rootobject);
}
getAllQuestions(){
返回此文件。http
.get(“/api/v1/xml”)
.map(res=>res[0].rootobject);
}
我认为它返回了一个Rootobject
,因为这样做会显示这个错误:“http”是angular2提供的标准服务吗?到目前为止,我还没有将它与get(…)这样的显式类型定义一起使用,对此我有点惊讶。通常我使用get(…).map(respone=>response.json()).Arg,真不敢相信这就是错误所在。非常感谢你!我按照您的回答更改了我要获取的对象,并在组件中将其更改为:this.questions=res[0]
,改为:this.questions=res[0]。rootobject
。不,http服务是一个自定义服务,内部使用了角度服务。对不起,我忘了提到这一点。不起作用:(这个。问卷
是一个对象(在本例中,是一个根对象
,而不是一个数组)。例如,如果我尝试访问res[0][1]
,它将导致一个错误,因为res[0]
仍然没有定义。对不起,我的错:/