-1
getEmployeeList():Observable<Employee[]>{
  return this.http.get<Employee[]>('moi/employee/getEmp', AuthService.getHttpOptions());
}

and my AuthService.getHttpOptions() method is:

 public static getHttpOptions(): any {''
    var httpOptions = {
        headers: new HttpHeaders({
            'Content-Type':  'application/json',
            'observe': "body",
            'Authorization': "Bearer " + localStorage.getItem("authtoken")
          })
    };
    return httpOptions;
}

Got the error:

Type 'Observable<HttpEvent<Employee[]>>' is not assignable to type 'Observable<Employee[]>'.

Type 'HttpEvent<Employee[]>' is not assignable to type 'Employee[]'.

Type 'HttpSentEvent' is not assignable to type 'Employee[]'.

Property 'includes' is missing in type 'HttpSentEvent'.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Anupam Choudhary
  • 39
  • 1
  • 1
  • 9

1 Answers1

0
import { Observable } from 'rxjs/Observable';
import {Http,Headers,RequestOptions }  from '@angular/http';
import 'rxjs/add/operator/map';
import { tokenNotExpired} from 'angular2-jwt';
export Class serviceName{
  authToken;
  headers;
  // After login we have already store jwt token on the localstorage
  constructor(private _http:Http,private _router:Router) { 
    this.loadToken();
    this.headers = new Headers({'Content-Type': 'application/json', 'Authorization': 
     'Bareer '+this.authToken});
   }
   loadToken(){
     this.authToken = localStorage.getItem('token');
   }
  createAuthenticateHeaders(){
    this.loadToken();
    this.options = new RequestOptions({
      'headers':new Headers({
        'Content-Type': 'application/json',
        'authorization':this.authToken
      })
    });
  }
   getEmployeeList(userInfo){
    this.createAuthenticateHeaders();
    return this._http.get(this.domain+'/products', {headers:this.headers})
     .map(response => response.json());
   }
 }

You can bind token like this way. Steps

  • Do login and generate authtoken
  • Store token in sessionstorage.enter code here
  • Then in constructor of a service load that token.
  • Then Create headers
  • Call any service method with passing token in the header.
Ravi Mariya
  • 1,210
  • 15
  • 19
shivam
  • 1