[英]ERROR Error: Uncaught (in promise): TypeError: Cannot set property 'empolyee' of undefined
[英]ERROR Error: Uncaught (in promise): TypeError: Cannot set property 'devices' of undefined in ionic 4 Angular
我有一個名為 MyDevicesPage 的類,它控制一個頁面,我試圖操作 res 對象,然后將其傳遞給 DataService 的 updateDevicesToServer 方法以進行進一步操作。 代碼編譯得很好,但在運行時它會拋出一個錯誤錯誤錯誤:未捕獲(承諾):類型錯誤:無法設置未定義的屬性“設備”
這是類和相關的接口
export class MydevicesPage implements OnInit {
devices : Array<deviceInterface>
constructor(private deviceService : DeviceService,private route: ActivatedRoute,private router: Router, private authenticationService : AuthenticationService) { }
ngOnInit() {
this.deviceService.getDevices().then((res : devicesInterface) => {
if(res){
let data : ResUpdateDevices
data.devices = res;
this.devices = res.devices;
data.token = this.authenticationService.getToken();
this.deviceService.updateDevicesToServer(data).subscribe(res => {
console.log(res)
},err=>{
console.log(err)
});
}
})
}
goto_device(ssid : String, name : String){
this.router.navigate(['members','device',ssid,name])
}
}
ResUpdateInterface 接口
export interface ResUpdateDevices{
devices : devicesInterface
token : string
}
設備接口接口
export interface deviceInterface {
ssid : String,
name : String
}
設備接口接口
export interface devicesInterface {
devices : Array<deviceInterface>
}
Console Logging res 給出了這個
{devices : [{ssid:"ssid", name :"name"}]}
您收到此錯誤是因為您在嘗試分配data.devices = res;
之前沒有初始化data
對象data.devices = res;
. 我建議進行以下更新:
let data: ResUpdateDevices = {
devices: res,
token: this.authenticationService.getToken(),
};
this.devices = res.devices;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.