Угловое тело получить из HttpEvent <> - PullRequest
1 голос
/ 03 мая 2019

Я выполняю запрос get, вызывая этот метод класса AuthService:

 return this.http.post<any>('url',{
    "username": username,
    "password":password
}, this.httpOptions).pipe(catchError(this.errorHandler));

из другого класса:

this.authService.login(this.username,this.password).subscribe(response =>{
       console.log("Response " + response.token);
    });

Проблема в том, что property token does not exist in type HttpEvent<any>,Если я запускаю его и все равно пытаюсь его напечатать, он печатается правильно, но ошибка в коде остается.Я попытался изменить тип any на пользовательский интерфейс с полями, представляющими мой ответ, но ошибка та же, потому что это всегда HttpEvent.
Как я могу решить эту проблему?

1 Ответ

1 голос
/ 03 мая 2019

попробуйте

this.authService.login(this.username,this.password).subscribe((response : any) =>{
       console.log("Response " + response.token);
    });

или, если хотите, наберите

return this.http.post<{token: string}>('url',{

, а затем

this.authService.login(this.username,this.password).subscribe((response : {token: string}) =>{
       console.log("Response " + response.token);
    });
...