update_user(user_to_update:any): Observable<any> {
let json = JSON.stringify(user_to_update);
let params = json;
let headers = new HttpHeaders({ 'Content-Type': 'application/json',
'Authorization': this.getToken()
});
return this._http.put(this.url 'update-user/' user_to_update._id, params, { headers: headers });
}
Me sale este error en el 'Authorization':
error TS2322: Type 'Observable' is not assignable to type 'string | string[]'. Type 'Observable' is missing the following properties from type 'string[]': length, pop, push, concat, and 27 more.
47 'Authorization': this.getToken()
CodePudding user response:
I imagine this.getToken return an Observable, and "Authorization" should be an string
Your function update_user return an observable, but this observable need the result of another one. So you should use switchMap rjxs operator.
It's not very complex. The idea is
return observable_one.pipe(
switchMap(res=>{
//in res you has the response
return observable_two
}))
In your case
update_user(user_to_update:any): Observable<any> {
let json = JSON.stringify(user_to_update);
let params = json;
return this.getToken().pipe(
switchMap((authorization:any)=>{
let headers = new HttpHeaders({ 'Content-Type': 'application/json',
'Authorization': authorization
});
return this._http.put(this.url 'update-user/' user_to_update._id,
params, { headers: headers });
})
)
}
NOTE: Really you needn't use JSON.stringify, Angular make it for you so really your function becomes like
update_user(user_to_update:any): Observable<any> {
return this.getToken().pipe(
switchMap((authorization:any)=>{
let headers = new HttpHeaders({ 'Content-Type': 'application/json',
'Authorization': authorization
});
return this._http.put(this.url 'update-user/' user_to_update._id,
user_to_update, { headers: headers });
})
)
}