出於某種原因,我無法訪問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'。你得到了什麼?你的輸出是否正確? –