没有运行观察到的地图功能(Angular2,HTTP) [英] Observable Map function not running (Angular2, http)

查看:378
本文介绍了没有运行观察到的地图功能(Angular2,HTTP)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新

这个问题似乎是,地图功能是不是一个失败的要求执行。这意味着,如果API我说要返回 422验证失败错误(或其他4XX错误)角将其视为故障,并导致观察员运行订阅错误回调,跳绳的过程中地图的功能。

The issue seems to be that the map function is not run on a "failed" request. This means that if the API I'm talking to returns a 422 Validation Failed error (or other 4xx errors) Angular will treat this as a failure and cause the Observer to run the subscribed error callback, skipping the map function in the process.

是否有可能迫使角度看待某些4XX错误,因为成功的请求或强制地图功能运行,即使可观察到的返回一个错误?

Is it possible to force Angular to treat certain 4xx errors as successful requests, or force the map function to run even when an Observable returns an error?

我有以下的code运行在我Angular2的应用程序:

I have the following code running in my Angular2 app:

import {Injectable} from "angular2/core";
import {Observable} from "rxjs/Rx";
import {Http, Headers, ResponseOptions, Response} from "angular2/http";
import 'rxjs/add/operator/map';

...

public login (email : string, password : string) {
    return this.http.post(_endPoint + '/auth/login/', JSON.stringify({
        email: email,
        password: password
    }), {
        headers: new Headers({
            'Content-Type': 'application/json'
        })
    })
    .map (res => {
        let data = res.json();
        console.log(data);
        return data;
    });
}

在code正在执行罚款,所不同的是地图功能没有被解雇。我没有得到任何错误,并试图既没有在导入运行code'rxjs /添加/运营/图具有相同的结果。我也尝试了简单的地图功能 .MAP(RES => res.json());

The code is executing fine, with the exception that the map function is not fired. I am not getting any errors, and have tried running the code both with and without the import 'rxjs/add/operator/map' with the same result. I have also tried a simpler map function .map (res => res.json());.

在这两种情况下,我期望在 .subscribe返回()函数得到JSON响应的结果,而是我得到的原始响应。

In both cases I expect the result returned in the .subscribe() function to be the JSON response, but instead I am getting the raw response.

编辑:添加请求数据的截图,并且响应

Added a screenshot of the request data, and the response

请求详细信息

响应:

[{"field":"email","message":"Email or password incorrect."},{"field":"password","message":"Email or password incorrect."}]

我也测试了它在一个完全成功的请求(状态code:200)和地图功能似乎是工作的罚款。所以我猜它只是返回成功响应时运行。有没有办法让无论它运行,或指定其他状态codeS,它应该运行呢?

I've also tested it on a completely successful request (Status Code: 200) and the map function appears to be working fine. So I guess it only runs when a successful response is returned. Is there a way to get it to run regardless, or specify additional status codes that it should run on?

推荐答案

与意见的讨论,不叫地图方法,因为响应包含422 $状态C $ C,即一个错误。因此,抓方法直接调用。

As discussed with comments, the map method isn't called because the response contains a 422 status code, i.e. an error. So the catch method is directly called.

如果你需要提取相应的错误JSON的内容,你可以尝试类似的东西在你的服务:

If you need to extract the JSON content corresponding to the error you could try something like that in your service:

getCompanies() {
  return this.http.get('https://angular2.apispark.net/v1/companies1/', {
    headers: headers
  }).map(res => res.json()).catch(err => Observable.throw(err.json());
}

现在在调用该服务的组件,您就可以订阅。

Now in the component that calls the service, you will be able to subscribe. The second parameter will be called in your case with the JSON content of the response:

service.getCompanies().subscribe(
  data => console.log('data = '+data),
  err => console.log('err = '+JSON.stringify(err, null, 2)), // <---
  () => console.log('complete')
);

打印的内容是:

err = {
  "code": 404,
  "description": "The server has not found anything matching the request URI",
  "reasonPhrase": "Not Found"
}

希望它可以帮助你,
蒂埃里

Hope it helps you, Thierry

这篇关于没有运行观察到的地图功能(Angular2,HTTP)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆