在这段代码中,我尝试从web服务中提取tripID和NoteID,并将它们存储在trips中,然后通过读取trips[i].noteID缓存其他web服务中的其他参数,将它们存储在其他函数中,但是会出现错误:
public trips= [{ "Token" : "" ,
"UDI" : "",
"tripID" : "",
"NoteID":"",
"START_ADDRESS":"",
"END_ADDRESS":"",
"NAME":"",
"PHONE":"",
"COST":"",
}];
GetTrips(){
let i = 0;
let Url='http://mysitesite/gettrip/'+this.UDI+'/'+this.Token;
console.log(Url);
this.http.get(Url)
.map(res => res.json())
.subscribe(data => {
console.log(data);
for(let note of data.notes) {
this.trips[i].Token=this.Token;
this.trips[i].UDI=this.UDI;
this.trips[i].NoteID=note.ID;
this.trips[i].tripID=note.TRIP;
i++;
}
console.log(this.trips);
}
});
}错误:
异常:无法设置未定义的属性“令牌” 异常:无法设置未定义的属性“UDI” 异常:无法设置未定义的属性“‘NoteID” 异常:无法设置未定义的属性“tripID”
更新1:这是@toskv之后的最新更改:
for(let note of data.notes) {
let newTrip = {
Token: this.Token,
UDI: this.UDI,
NoteID: note.ID,
tripID: note.TRIP,
START_ADDRESS :null,
END_ADDRESS:null,
NAME:null,
PHONE:null,
COST:null,
};
this.trips.push(newTrip);
}
console.log(this.trips);
this.trips =this.trips.slice(1);
console.log(this.trips.length);
} 发布于 2017-04-30 20:14:00
获取数组中不存在的索引的值将导致返回未定义的值。
相反,您应该在数组中推送新值。
GetTrips() {
let Url = 'http://mysitesite/gettrip/' + this.UDI + '/' + this.Token;
console.log(Url);
this.http.get(Url)
.map(res => res.json())
.subscribe(data => {
console.log(data);
for (let note of data.notes) {
let newTrip = {
Token: this.Token,
UDI: this.UDI,
NoteID: note.ID,
tripID: note.TRIP
};
this.trips.push(newTrip);
}
console.log(this.trips);
}
});
}https://stackoverflow.com/questions/43710320
复制相似问题