ОШИБКА Ошибка: Uncaught (в обещании): TypeError: Невозможно установить свойство 'devices' из неопределенного в ionic 4 Angular - PullRequest
0 голосов
/ 25 февраля 2019

У меня есть класс MyDevicesPage, который управляет страницей, которой я пытаюсь манипулировать объектом res, а затем передаю ее методу updateDevicesToServer объекта DataService для дальнейших действий.код компилируется нормально, но во время выполнения выдает ошибку ОШИБКА Ошибка: Uncaught (в обещании): TypeError: Невозможно установить свойство 'devices' из неопределенного

здесь класс и связанные интерфейсы

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
}

Интерфейс DeviceInterface

export interface deviceInterface {
  ssid : String,
  name : String
}

Интерфейс DevicesInterface

export interface devicesInterface {
  devices : Array<deviceInterface>  
}

Res Logging res дает это

{devices : [{ssid:"ssid", name :"name"}]}

1 Ответ

0 голосов
/ 25 февраля 2019

Вы получаете эту ошибку, потому что вы не инициализируете свой data объект, прежде чем пытаться присвоить data.devices = res;.Я предлагаю следующее обновление:

let data: ResUpdateDevices = {
    devices: res,
    token: this.authenticationService.getToken(),
};
this.devices = res.devices;
...