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

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

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

I got the same problem. Someone answer me. Thanks

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

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:

  "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.

1 Like