The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

526865 (2) [Avatar] Offline
#1
Hi all,

I am new to Angular 2. I have an issue which I can't find the solution.
I am trying to develop a system which parses a file and send line by line (after parsing as json) to server.
I have a loop which runs on the file and place a http.post request per line.
In case of success, a second http.post to a different api is done.
In case of failure of the first call, i want to print the error and the line in the file caused the error.
The problem is that since this is a asynch call, i can't make the second http.post because the response return only after all calls are done. When trying to get the row of the error, I get the last row in the file. snip code:


for (var row = 0; row < rows.length; row++) {
    let strBody = JSON.stringify(row);
    res = this.http.post(url1, strBody, options)
    if(success) {
        strBody = JSON.stringify(res);
        this.http.post(url2, strBody, options)       
        if(fail) {
            //add error to table in html including row and strBody + reason
            //the problem is when reaching this location row = rows.length 
        }
    } else {
        //add error to table in html including row and strBody + reason
        //the problem is when reaching this location row = rows.length 
    }
}


I understand that the reason is the async nature of the call.
I appreciate your help
Yakov Fain (219) [Avatar] Offline
#2
Not sure what "if success" means. You need to subscribe to each post request the same way as you'd to with get requests. Also, use HttpClient instead of Http. See here: https://alligator.io/angular/httpclient-intro/
526865 (2) [Avatar] Offline
#3
"if success" means if response from server is OK (200).

for (var i = 0; i < 3; i++) {
      var t = i;
      const body = { name: t };
      this.http.post(url1, body).subscribe(
        data => {
          console.log('t:' + t); //always 2
          console.log('ok:');

          //body name is always 2
          this.http.post(url2, body).subscribe(
            data => {
              console.log('t:' + t);  //always 2
              console.log('ok:');
            },
            (err: HttpErrorResponse) => {
              if (err.error instanceof Error) {
                // A client-side or network error occurred. Handle it accordingly.
                console.log('t:' + t); //always 2
                console.log('An error occurred:', err.error.message);
              } else {
                // The backend returned an unsuccessful response code.
                // The response body may contain clues as to what went wrong,
                console.log('t:' + t); //always 2
                console.log(`Backend returned code ${err.status}, body was: ${err.error}`);
              }
            }
            );
        },
        (err: HttpErrorResponse) => {
          if (err.error instanceof Error) {
            // A client-side or network error occurred. Handle it accordingly.
            console.log('t:' + t);//always 2
            console.log('An error occurred:', err.error.message);
          } else {
            // The backend returned an unsuccessful response code.
            // The response body may contain clues as to what went wrong,
            console.log('t:' + t);//always 2
            console.log(`Backend returned code ${err.status}, body was: ${err.error}`);
          }
        }
        );
    }

the above example is the issue. in the error, i always get t =2. I would like to get the t for the current iteration of the loop


Thanks,
Yakov Fain (219) [Avatar] Offline
#4
Get rid of nested subscriptions using the flatMap() operator.