简体   繁体   English

如何在打字稿中的http发布响应中解包对象?

[英]How to unwrap object in http post response in typescript?

How do I unwrap an object received from an HTTP POST request? 如何解开从HTTP POST请求收到的对象?

Here is my code: 这是我的代码:

this.http.post(url, user, httpOptions).subscribe(result => {
  console.log(result.status);
});

I am getting the error on the console.log() 我在console.log()上收到错误

Property 'status' does not exist on type 'Object'.

How do I unwrap the result variable/response object so I can read the individual values in typescript? 如何解开结果变量/响应对象,以便可以读取打字稿中的各个值?

If I console.log(result), I can see all the values. 如果我console.log(result),我可以看到所有值。

You need to specify observe: 'response' if you want the whole HttpResponse in your httpOptions . 如果要在httpOptions使用整个HttpResponse ,则需要指定observe: 'response' The default behaviour is observe: 'body' , which returns an Observable of the response body. 默认行为是observe: 'body' ,它返回响应主体的Observable This is also described here. 这也在此处描述

I created a StackBlitz demonstrating how this should work (see console output). 我创建了一个StackBlitz,演示了它如何工作(请参阅控制台输出)。

this.http.post(url, user, {
   ...httpOptions, // or specifiy it inside httpOptions directly
   observe: 'response'
}).subscribe(result => {
  console.log(result.status);
});

The error you are getting is a TypeScript error though. 您得到的错误是TypeScript错误。 The response is inferred as type Object - which obviously doesn't have any status property. 响应被推断为Object类型-显然没有任何status属性。 Usually, the types should be correct. 通常,类型应该正确。 You could go around the issue by typecasting the response to any . 您可以通过将响应类型转换为any来解决该问题。

this.http.post(url, user, httpOptions).subscribe(result => {
  console.log((result as any).status); // Take the type information away for this statement
});

To be honest, I wouldn't suggest doing that though. 老实说,我不建议这样做。 You should investigate why the response is inferred as Object in the first place. 您应该首先调查为什么将响应推断为Object

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

相关问题 如何从打字稿中获取http响应 - how to get http response from typescript post 将 http 响应映射到 typeScript object - Mapping http response to typeScript object 如何从 HTTP 响应转换为可以在 Typescript 中使用的 object? - How to convert from an HTTP response to an object that I can use in Typescript? 打字稿typecheck不适用于Http发布响应 - Typescript typecheck not working on Http post response Angular 4 Http Post将响应放入对象 - Angular 4 Http Post put response in object 将响应从HTTP发布转换为Typescript对象列表 - Casting Response from HTTP Post to List of Typescript Objects HTTP POST请求失败,使用打字稿在Angular 2中以空值作为响应 - HTTP POST request failed with response as null values in angular 2 using typescript 如何将 http 请求响应映射到我在 TypeScript 中定义的对象 - How do I map http request response to my defined object in TypeScript 如何使用Angular2中的Observable Map函数将Http Service返回的Response对象映射到TypeScript对象 - How to Map Response object returned by Http Service to a TypeScript Objects using Observable Map function in Angular2 Http POST 响应未定义 - 如何使用 Ok(object) 方法从 controller 获取 object - Http POST response undefined - how to get an object from controller by using Ok(object) method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM