angular - Proprietatea nu există pe tipul Object în Typescript
Folosesc Ionic și Angular2 TypeScript. Când Vizualizarea este redată, apelez funcția retrieveNotifications în fișierul dashboard.ts, dar primesc o eroare:
Error: Error at /home/invicta/Desktop/newauth/.tmp/pages/dashboard/dashboard.ts:36:34 Property 'notifications' does not exist on type '{}'
//dashboard.ts
ionViewDidEnter() {
this.retrieveNotifications();
}
retrieveNotifications() {
this.user.retrieveNotifications().then(data = > {
// when do console.log here and typeof(), data is object and it has notifications array
this._notifications = data.notifications;
})
}
Apoi, în fișierul user-data.ts care este declarat ca public user: UserData există funcția:
//user-data.ts -> provider
retrieveNotifications() {
var token = this.authservice.getUserToken();
return new Promise(resolve = > {
var headers = new Headers();
headers.append('Authorization', 'Bearer ' + token);
this.http.get(this.mainUrl + '/api/v1/notification', {
headers: headers
}).subscribe(data = > {
if (data.status === 200)
resolve(data.json());
else
resolve(false);
});
});
}
Răspuns #1
Încearcă să ocolești eroarea cu:
this._notifications = data['notifications'];
Răspunsul #2
Încearcă o altă metodă pentru a ocoli eroarea cu:
return new Promise().then(()=>{
return data;
})
