简体   繁体   English

Angular-多个订阅引发错误

[英]Angular - multiple subscription throw error

I would like to create this functionality: 我想创建此功能:

  1. call API for return items 调用API获取退货项目
  2. items.length > 0 -> call next API items.length> 0->调用下一个API
  3. items.length == 0 -> call another API items.length == 0->调用另一个API

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

let a = this.http.get("URL");
a.subscribe(response => {
  if(response.data.length == 0)
    Observable.throw({});
});

a.subscribe(x => {console.log("CALL NEXT API")}, error => { console.log("ANOTHER API")}

This code allways displayed "CALL NEXT API", never call error function in subscribe. 此代码始终显示“ CALL NEXT API”,从不调用订阅中的错误函数。 What is the best solution for call multiple requests consecutively and with condition? 连续且有条件地调用多个请求的最佳解决方案是什么?

If this is your code 如果这是你的代码

let a = this.http.get("URL");
a.subscribe(response => {
  if(response.data.length == 0)
    Observable.throw({});
});

a.subscribe(x => {console.log("CALL NEXT API")}, error => { console.log("ANOTHER API")}

Stop wondering why it doesn't work. 不要再怀疑为什么它不起作用了。

Let me break it up to you. 让我把它交给你。

First, you make an HTTP call : 首先,您进行HTTP调用:

let a = this.http.get("URL");

Nothing bad here. 这里还不错。 Then, you subscribe to it once : 然后,您订阅一次:

a.subscribe(response => {
  if(response.data.length == 0)
    Observable.throw({});
});

Here, put into words, you do this : 简而言之,您可以执行以下操作:

(nothing) (没有)

That's right, you do nothing . 是的,你什么都不做。 All you do is creating an error observable, that you don't bind to a variable. 您要做的只是创建一个可观察到的错误,即您没有绑定到变量。 Basically, you do nothing. 基本上,您什么都不做。

Then, you do a second subscription : 然后,您进行第二次订阅:

a.subscribe(x => {console.log("CALL NEXT API")}, error => { console.log("ANOTHER API")}

Put into words, this gives 换句话说,这给了

If the call is successful, log "CALL NEXT API", and if there is an error, log "ANOTHER API". 如果调用成功,则记录“ CALL NEXT API”,如果出错,则记录“ ANOTHER API”。

If you always see CALL NEXT API , then I assume you only make successful calls. 如果您始终看到CALL NEXT API ,那么我想您只能进行成功的调用。

If your goal is to make successive API calls, you should use the mergeMap operator like so : 如果您的目标是进行连续的API调用,则应使用mergeMap运算符,如下所示:

this.http.get('URL')
  .pipe(mergeMap(response1 => {
    this.http.get(response1.data.length ? 'SUCCESS URL' : 'EMPTY ARRAY URL').subscribe(response2 => {
      /* ... */
    });
  }));

I may have misunderstood the question, but couldnt you just call other APIs in the subscribe callback? 我可能误解了这个问题,但是您能否只在订阅回调中调用其他API?

this.http('URL').subscribe(response => {
    if (response.data.length == 0) {
        console.log('Call another API');
    }
    else {
        console.log('Call next API');
    }
}, error => {
    console.log('Could not return result');
}

If the api is called successfully, it will return a result even if the length is 0. Then you can call the relevant API. 如果成功调用了api,即使长度为0,它也会返回结果。然后,您可以调用相关的API。

It should only hit the error callback if there was a problem accessing the URL or bad data formatting etc. 仅当访问URL或数据格式错误等问题时,才应返回错误回调。

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

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