简体   繁体   English

错误类型错误:无法读取未定义的属性(读取“管道”)

[英]ERROR TypeError: Cannot read properties of undefined (reading 'pipe')

Why do I get an error (ERROR TypeError: Cannot read properties of undefined (reading 'pipe')) in this case when I add to the request "{ observe: 'response' }"?在这种情况下,当我添加到请求“{观察:'响应'}”时,为什么会出现错误(错误类型错误:无法读取未定义的属性(读取“管道”))? For to get all headers.获取所有标题。

    let answer = this.http.post<ResponseLoginHttp>(AUTH_API + "login", json_data, { observe: 'response' });
let answer2
answer.subscribe(value => {
  this.dataService.setResponceLoginHttp(value.body);
  answer2 = value.body
})
return answer2.pipe(
  map((data) => {
    return data;

  }),
  catchError((error) => {
    console.log("Error - ", error);
    throw new Error(error.message);
  })
);}

In case I don't use "{ observe: 'response' }" Everything is fine:如果我不使用“{观察:'响应'}”一切都很好:

    let answer = this.http.post<ResponseLoginHttp>(AUTH_API + "login", json_data );
answer.subscribe(value => {
  this.dataService.setResponceLoginHttp(value);
})
return answer.pipe(
  map((data) => {
    return data;

  }),
  catchError((error) => {
    console.log("Error - ", error);
    throw new Error(error.message);
  })
);}

how can i get all the headers from the response without having an error "ERROR TypeError: Cannot read properties of undefined (reading 'pipe')"?如何从响应中获取所有标头而不会出现错误“错误类型错误:无法读取未定义的属性(读取'管道')”?

this has nothing to do with your {observe: 'response'}这与您的{observe: 'response'}无关

When you do this :当你这样做时:

let answer = this.http.post<ResponseLoginHttp>(AUTH_API + "login", json_data, { observe: 'response' });
let answer2
answer.subscribe(value => {
  this.dataService.setResponceLoginHttp(value.body);
  answer2 = value.body
})
return answer2.pipe(

You call answer2 outside the subscribe and your answer2 is not yet defined.您在订阅之外调用answer2并且您的 answer2 尚未定义。 The subscribe is an async tool, js will not wait the response to execute the next lines. subscribe 是一个异步工具,js 不会等待响应来执行下一行。 Plus your answer2 will never be an Observable .另外,您的answer2永远不会是Observable

You have to do something like this:你必须做这样的事情:

this.http.post<ResponseLoginHttp>(AUTH_API + "login", json_data, { observe: 'response' })
    .pipe(
        map((response: HttpResponse<ResponseLoginHttp>) => {
            // Here you can do something with response.headers
            return response.body;
        })      
     )
    .subscribe(
        value => this.dataService.setResponceLoginHttp(value),
        error => {
            console.log("Error - ", error);
            throw new Error(error.message);
        });

Or simpler:或更简单:

this.http.post<ResponseLoginHttp>(AUTH_API + "login", json_data, { observe: 'response' })
    .subscribe(
        response => {
            // Here you can do something with response.headers
            this.dataService.setResponceLoginHttp(response.body);
        }),
        error => {
            console.log("Error - ", error);
            throw new Error(error.message);
        });

暂无
暂无

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

相关问题 错误:无法读取未定义的属性(读取“管道”)Angular12 - Error: Cannot read properties of undefined (reading 'pipe') Angular12 TypeError:无法读取未定义(读取“过滤器”)和未定义错误的属性 - React - TypeError: Cannot read properties of undefined (reading 'filter') and undefined error - React 未定义错误:TypeError:无法读取未定义的属性(读取“图标”) - Undefined Error: TypeError: Cannot read properties of undefined (reading 'Icon') 错误无法读取未定义的属性(读取“配置”)。 TypeError:无法读取未定义的属性(读取“配置”) - ERROR Cannot read properties of undefined (reading 'configurations'). TypeError: Cannot read properties of undefined (reading 'configurations') 错误:TypeError:无法读取未定义的属性(读取“transformFile”)本机反应 - error: TypeError: Cannot read properties of undefined (reading 'transformFile') react native 错误:未捕获(承诺中)类型错误:无法读取未定义的属性(读取“数据”) - ERROR: Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'data') 错误:未捕获的类型错误:无法读取未定义的属性(读取“地图”) - Error: Uncaught TypeError: Cannot read properties of undefined (reading 'map') “TypeError:无法读取未定义的属性(读取&#39;hasOwnProperty&#39;)”错误 - "TypeError: Cannot read properties of undefined (reading 'hasOwnProperty')" error Angular 12:错误类型错误:无法读取未定义的属性(读取“nativeElement”) - Angular 12: ERROR TypeError: Cannot read properties of undefined (reading 'nativeElement') 在 reactjs TypeError 中出现错误:无法读取未定义的属性(读取“过滤器”) - Getting error in reactjs TypeError: Cannot read properties of undefined (reading 'filter')
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM