简体   繁体   English

Angular 6 Http订阅问题:类型“ Response”上不存在属性“ userInfo”

[英]Angular 6 Http Subscribe issue: Property 'userInfo' does not exist on type 'Response'

I am using angular6 我正在使用angular6

Service.ts Service.ts

post(url, body, options): Observable<Response>{
        let headers = new Headers();
        this.createAuthorizationHeader(headers);
        return this.http.post(this.endpoint + url, body, httpOptions)
        .pipe(map(this.parseData))
        .pipe(catchError(this.handleError));
    }

loginService.ts loginService.ts

login(data) {
        return this.httpClient.post('api/authenticate', data , {
            'Content-Type': 'application/x-www-form-urlencoded'
        });
    }

login.ts login.ts

onSubmit() {
        if(this.loginForm.invalid) {
            return;
        }else{
            let data = {
                'email': this.loginForm.value.email,
                'password': this.loginForm.value.password
            }
            this.loginService.login(data).subscribe(
                response => {

console.log(response.userInfo.name);

                this.myRoute.navigate(['dashboard']);
            },
            error =>{
                console.log(error);
            }
        )
    }
}

I am getting error Property 'userInfo' does not exist on type 'Response'. 我收到错误消息Property 'userInfo' does not exist on type 'Response'. but in my http response i am {"status":true,"userInfo":{"id":1,"email":"admin@admin","password":"123456","type":"admin","name":"Admin"},"message":"su"} 但在我的http响应中,我是{"status":true,"userInfo":{"id":1,"email":"admin@admin","password":"123456","type":"admin","name":"Admin"},"message":"su"}

I even tired remove my map function i am still getting the same error for the subscribe function. 我什至不愿意删除我的地图功能,但订阅功能仍然遇到相同的错误。 Angular 2 i am not getting this kind of issue. Angular 2我没有遇到这种问题。 Post update to angular 6 i am getting this issue i even updated import { map, catchError, tap } from 'rxjs/operators'; 发布更新到angular 6我遇到了这个问题,甚至更新了import { map, catchError, tap } from 'rxjs/operators'; the import file. 导入文件。

Sorry bad english. 对不起英语不好。 Any update or insight is appreciated. 任何更新或见解表示赞赏。 Thanks 谢谢

您可以使用response['userInfo']默认响应没有名为userInfo的属性,您可以将响应类型强制转换为给定类型或执行上述操作。

If I guess correctly from the question, the error is raised at compile time. 如果我从问题中正确猜出,则在编译时会引发错误。 Did you define correctly a Response type ? 您是否正确定义了响应类型? It should look like 它看起来像

type Response = {
  status: boolean,
  userInfo: {id: number, email: string, password: number, type: string, name: string},
  message: string};

Or, if you are going to use your post() function for other types of requests, define a more generic post function in your service, as follows: 或者,如果您打算将post()函数用于其他类型的请求,请在服务中定义一个更通用的post函数,如下所示:

post(url, body, options): Observable<any> { ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM