I'm trying to follow the tutorial here but since I'm using a newer version of Angular and Ionic (Angular 5 and Ionic 3), I got errors on these lines below
this.token = data.token;
this.storage.set('token', data.token);
Visual Studio code display this error:
Property token does not exist on type 'Object'
The code for the function is as below:
createAccount(details){
return new Promise((resolve, reject) => {
let headers = new Headers();
headers.append('Content-Type', 'application/json');
this.http.post('https://YOUR_HEROKU_APP.herokuapp.com/api/auth/register', JSON.stringify(details), {headers: headers})
.subscribe(res => {
let data = res.json();
this.token = data.token;
this.storage.set('token', data.token);
resolve(data);
}, (err) => {
reject(err);
});
});
}
What should I change in my code?
Try parsing it as,
this.token = data['token'];
this.storage.set('token', this.token);
If Typescript not able to identify the property 'token' on your object 'res' change to res: any
this.http.post(api.url, params)
.subscribe((res:any) => {
...
..
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With