TypeError: is not a function in TypeScript class

Discussão em 'Angular' iniciado por Aaron Balthaser, Novembro 28, 2025.

  1. I'm getting the following error in my TypeScript class. I am trying to call a helper function passing the token.

    Error:


    post error: TypeError: this.storeToken is not a function(…)
    ​

    Class:

    /**
    * Authentication Service:
    *
    * Contains the http request logic to authenticate the
    * user.
    */
    import { Injectable } from '@angular/core';
    import { Http, Response, Headers, RequestOptions } from '@angular/http';

    import 'rxjs/Rx';
    import { Observable } from 'rxjs/Observable';

    import { AuthToken } from './auth-token.service';

    import { User } from '../../shared/models/user.model';

    @Injectable()
    export class Authenticate {

    constructor(
    private http: Http,
    private authToken: AuthToken
    ) {}

    post(user: User): Observable<any> {
    let url = 'http://localhost:4000/';
    let body = JSON.stringify(user);
    let headers = new Headers({ 'content-type': 'application/json' });
    let options = new RequestOptions({ headers: headers });

    return this.http.post(url + 'login', body, options)
    .map(this.handleData)
    .catch(this.handleError);
    }

    private storeToken(token: string) {
    this.authToken.setToken(token);
    }

    private handleData(res: Response) {
    let body = res.json();
    this.storeToken(body.token);
    return body.fields || {};
    }

    private handleError(error: any) {
    console.error('post error: ', error);
    return Observable.throw(error.statusText);
    }
    }

    Continue reading...

Compartilhe esta Página