As I tried to create user login in Angular-13, I have this model:
export interface IUser {
email: string;
token: string;
}
service:
export class AccountService {
baseUrl = environment.apiUrl;
private currentUserSource = new ReplaySubject<IUser>(1);
currentUser$ = this.currentUserSource.asObservable();
constructor(private http: HttpClient, private router: Router) { }
login(values: any) {
return this.http.post(this.baseUrl 'account/login', values).pipe(
map((user: IUser) => {
if (user) {
localStorage.setItem('token', user.token);
this.currentUserSource.next(user);
}
})
);
}
}
I got this error while trying to implement it:
Argument of type 'OperatorFunction<IUser, void>' is not assignable to parameter of type 'OperatorFunction<Object, void>'
This is highlighted:
map((user: IUser) => {
How do I resolve this?
Thanks
CodePudding user response:
As you expect that your response returned is IUser
type, then specify T
as IUser
type.
post<T>(url: string, body: any, options?: { headers?: HttpHeaders | { [header: string]: string | string[]; }; context?: HttpContext; observe?: "body"; params?: HttpParams | { [param: string]: string | number | boolean | ReadonlyArray<string | number | boolean>; }; reportProgress?: boolean; responseType?: "json"; withCredentials?: boolean; }): Observable<T>
login(values: any) {
return this.http.post<IUser>(this.baseUrl 'account/login', values).pipe(
map((user: IUser) => {
...
})
);
}