Node.js TypeError:无法设置属性';地址';在SafeSubscriber上未定义的的。\u下一步
我试图将数据传递到组件内部声明的数组,但我只获取数组的第一个索引,其他元素不返回 我读到程序是异步工作的,只得到第一个索引,他们还建议在for循环中更改var I,让I改变它 组成部分:Node.js TypeError:无法设置属性';地址';在SafeSubscriber上未定义的的。\u下一步,node.js,angular,Node.js,Angular,我试图将数据传递到组件内部声明的数组,但我只获取数组的第一个索引,其他元素不返回 我读到程序是异步工作的,只得到第一个索引,他们还建议在for循环中更改var I,让I改变它 组成部分: weathers: Weather[] = [{adress:'',city:''}]; weather: Weather; constructor(private weatherService: WeatherService) { } ngOnInit() { this.weatherServic
weathers: Weather[] = [{adress:'',city:''}];
weather: Weather;
constructor(private weatherService: WeatherService) {
}
ngOnInit() {
this.weatherService.getWeather().subscribe(weathers =>
{
let size = (Object).keys(weathers).length;
this.weathers = Weather[size] = [{adress:'',city:''}];
console.log(weathers);
console.log(size);
for(let i=0;i<size;i++) {
const adress = weathers[i].adress;
const city = weathers[i].city;
this.weathers[i].adress=adress;
this.weathers[i].city=city;
}
}
);
}
错误类型错误:
Cannot set property 'adress' of undefined
at SafeSubscriber._next (weather.component.ts:31)
at SafeSubscriber.__tryOrUnsub (Subscriber.js:185)
at SafeSubscriber.next (Subscriber.js:124)
您不应该以这种方式使用数组:
this.weathers=Weather[size]=[{地址:'',城市:'}]代码>。制作阵列时,无需设置阵列的大小:
let size = (Object).keys(weathers).length;
this.weathers = [];
for(let i=0;i<size;i++) {
const adress = weathers[i].adress;
const city = weathers[i].city;
this.weathers.push({'adress': adress, 'city': city});
}
Cannot set property 'adress' of undefined
at SafeSubscriber._next (weather.component.ts:31)
at SafeSubscriber.__tryOrUnsub (Subscriber.js:185)
at SafeSubscriber.next (Subscriber.js:124)
let size = (Object).keys(weathers).length;
this.weathers = [];
for(let i=0;i<size;i++) {
const adress = weathers[i].adress;
const city = weathers[i].city;
this.weathers.push({'adress': adress, 'city': city});
}
console.log(weathers);
this.weathers = (Object).keys(weathers).map(key => {
return {
adress: weathers[key].adress,
city: weathers[key].city
}
}