Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Property token does not exist on type Object

Tags:

angular

ionic3

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?

like image 965
imin Avatar asked Feb 13 '18 17:02

imin


Video Answer


2 Answers

Try parsing it as,

  this.token = data['token'];
  this.storage.set('token', this.token);
like image 187
Sajeetharan Avatar answered Oct 11 '22 20:10

Sajeetharan


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) => { 
...
..
}
like image 20
Diptendu Das Avatar answered Oct 11 '22 21:10

Diptendu Das