# ERROR TypeError: Cannot read property 'token' of undefined (ionic 4)

**URL:** <https://forum.ionicframework.com/t/error-typeerror-cannot-read-property-token-of-undefined-ionic-4/166063>\
**Category:** ionic-v3\
**Created:** [June 12, 2019, 9:18am UTC](https://forum.ionicframework.com/t/error-typeerror-cannot-read-property-token-of-undefined-ionic-4/166063 "2019-06-12T09:18:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![valueice](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/valueice/32/111060_2.png) [@valueice](https://forum.ionicframework.com/u/valueice)\
**Post date:** [June 12, 2019, 9:18am UTC](https://forum.ionicframework.com/t/error-typeerror-cannot-read-property-token-of-undefined-ionic-4/166063/1 "2019-06-12T09:18:50Z")

</div>

I and trying to build an using a php API, I have been able to login, but when I tried to access other endpoints like logout I get the error “Preformatted textERROR TypeError: Cannot read property ‘token\_type’ of undefined”

below is my code

**auth.services TS**

```auto
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { tap } from 'rxjs/operators';
import { ApiService } from './api.service';
import { User } from '../models/user';
import { NativeStorage } from '@ionic-native/native-storage/ngx';

@Injectable({
  providedIn: 'root'
})
export class AuthService {

  isLoggedIn = false;
  token:any;

  constructor(
    private http: HttpClient,
    private api: ApiService,
    private storage: NativeStorage,
  ) { }

  login(account_number: Number, password: String) {
    return this.http.post(this.api.API_URL + '/login',
      {account_number: account_number, password: password}
    ).pipe(
      tap(token => {
        this.storage.setItem('token', token)
        .then(
          () => {
            console.log('Token Stored', token);
          },
          error => console.error('Error storing item', error)
        );
        this.token = token;
        this.isLoggedIn = true;
        return token;
      }),
    );
  }
  register(name: String, email: String, phone: Number, reference: String, account_number: String, password: String) {
    return this.http.post(this.api.API_URL + '/register',
      {name: name, email: email, phone: phone, reference: reference, account_number: account_number, password: password}
    )
  }
  logout() {
    const headers = new HttpHeaders({
      'Authorization': this.token["token_type"]+" "+this.token["access_token"]
    });
    return this.http.get(this.api.API_URL + '/logout', { headers: headers })
    .pipe(
      tap(data => {
        this.storage.remove("token");
        this.isLoggedIn = false;
        delete this.token;
        return data;
      })
    )
  }
  user() {
    const headers = new HttpHeaders({
      'Authorization': this.token["token_type"]+" "+this.token["access_token"]
    });
    return this.http.get<User>(this.api.API_URL + '/account_balance', { headers: headers })
    .pipe(
      tap(user => {
        return user;
       
      })
    )
  }
  getToken() {
    return this.storage.getItem('token').then(
      data => {
        this.token = data;
        if(this.token != null) {
          this.isLoggedIn=true;
        } else {
          this.isLoggedIn=false;
        }
      },
      error => {
        this.token = null;
        this.isLoggedIn=false;
      }
    );
  }
}

```

the error

 ![error](https://global.discourse-cdn.com/ionicframework/original/3X/c/8/c83a818c9b373dfb002718defb65f2f9d898ea2b.png)

---

<div class="post-metadata">

**Author:** ![minthiha97](https://avatars.discourse-cdn.com/v4/letter/m/91b2a8/32.png) [@minthiha97](https://forum.ionicframework.com/u/minthiha97)\
**Post date:** [September 2, 2019, 4:33am UTC](https://forum.ionicframework.com/t/error-typeerror-cannot-read-property-token-of-undefined-ionic-4/166063/3 "2019-09-02T04:33:20Z")

</div>

I got the same problem. Someone answer me. Thanks

---

<div class="post-metadata">

**Author:** ![rapropos](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/rapropos/32/100227_2.png) [@rapropos](https://forum.ionicframework.com/u/rapropos)\
**Post date:** [September 2, 2019, 10:09pm UTC](https://forum.ionicframework.com/t/error-typeerror-cannot-read-property-token-of-undefined-ionic-4/166063/4 "2019-09-02T22:09:01Z")

</div>

As the error message says, you are executing something like:

```auto
thingy.foo

```

…where `thingy` is not an object with a `foo` property, but instead it’s `undefined`. I like to add the following to my `tsconfig.json`:

```auto
  "noImplicitAny": true,
  "strictNullChecks": true,
  "strictPropertyInitialization": true

```

This helps me catch virtually all bugs of this type as I write, instead of at runtime. You need to guarantee that objects are what you think they are when you try to access things within them.
