出于某种原因,我无法访问Angular项目中的请求响应。即使我可以在控制台上打印,我也可以访问data.status
和data.response
,但是当我尝试data.response.entries
时,出现以下问题。无法从角度请求中访问返回的数据
我的组件:
transportantions: any[]; //this is on the start of my component together with the rest of my variables
getTransportations() {
let loader = this.loadingController.create({
content: 'Getting data...'
});
loader.present();
this.wpApi.getTransportations()
.then(function (data) {
console.log(data);
if (data.status == 200) {
this.transportantions = data.response.entries;
loader.dismiss();
} else {
console.log('Something was wrong. Error status ' + data.status);
}
})
.catch(function (err) {
loader.dismiss();
console.log('something was wrong: ' + err);
});
}
这是console.log(data)
{
"status": 200,
"response": {
"total_count": "242",
"entries": [
{
...
},
{
...
},
{
...
},
...
]
}
}
输出而我得到的错误是:
something was wrong: TypeError: Cannot set property 'transportantions' of undefined
检查'data.response'。你得到了什么?你的输出是否正确? –