【发布时间】:2017-09-28 08:24:00
【问题描述】:
在此代码中,我尝试从 Web 服务中获取 tripID 和 NoteID 并将它们存储在行程中,然后通过读取 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);
}
【问题讨论】:
-
您应该真正了解一下数组在 js 中的工作原理。现在你在 for. 里面做 undefined.Token
标签: arrays typescript ionic2