0

I'm calling an API in Angular and trying to get the response body, but it just displays error message is [object Object]

I've tried just doing console.log(error), error.text() ,JSON.parse(error._body).errors ,error.error ,error.message and error.json()but none worked.

This is my code, would really appreciate some help:

  doImport = (dataObj) => {
    for(let i = 0; i<this.uploadedFilesCount; i++) {
      const filename = this.uploadedFileNames[i];
      this.disableDeployCTA = true;
      const params:string = filename + "/" + this.testStartTime + "/" + this.userNtId + "/" + this.userTeamID + "/" + this.userTeam; 
      var res = '';
      this.httpService.importUML(params).subscribe((response: any) => {        
        this.toastmessage.success('Successfully created test #'+i); 
        this.toastmessage.clear();
      },                              
      (error) => {
        console.log("error message is "+error.message);
        if(error.status == 409) {
          if(res.indexOf("static data")!=-1) {
            this.toastmessage.error(filename + ": That static data exists already!");
          }
          else {
            this.toastmessage.error(filename + ": That test case exists already! Please change test case name.");
          }
        }
        else if(error.status == 400) {
          this.toastmessage.error(filename + ": Incorrectly formatted UML. Please fix and try again.");
        }
      });
    }
    this.resetAxiForm();
    this.disableImport = true; 
  } 

And the error response I want to get:

enter image description here

When I do console.log(error): enter image description here

anon comp
  • 143
  • 11

1 Answers1

2

That's pretty common mistake in console.log, what's happening here is that the error you are receiving is actually an object, and what you are doing with that + sign is concatenating string with an object which will surely display [object Object], what you need to do is use a comma , instead of + inside console.log().

Replace console.log("error message is "+error.message); with console.log("error message is ", error.message);

Hope this helped.

Mohamed Karkotly
  • 1,364
  • 3
  • 13
  • 26
  • that did help, thank you! Sadly it still doesn't receive the response body, It just displays "axis.component.ts:507 error message is Http failure response for [server]: .409 Conflict". Do you happen to know how to retrieve that response? – anon comp Aug 11 '21 at 23:20
  • @anoncomp - Are you logging the entire `error object` not only the `error.message`? – Mohamed Karkotly Aug 11 '21 at 23:23
  • @anoncomp - Try logging the entire error object and paste the error here please – Mohamed Karkotly Aug 11 '21 at 23:24
  • @anoncomp - are you using a `PUT` request in your `service`? Could you please share your `HttpService` code as well. – Mohamed Karkotly Aug 11 '21 at 23:35
  • its a POST request! I pasted the image of the error object when I logged just the object. The POST method in httpService is `importUML(params) { return this.httpClient.post('http://localhost:8081/bruml/import/' + params, ""); }` – anon comp Aug 11 '21 at 23:41
  • @anoncomp - Here's what i found regarding this problem (https://stackoverflow.com/questions/6785036/reasons-for-a-409-conflict-http-error-when-uploading-a-file-to-sharepoint-using) hope this help you. – Mohamed Karkotly Aug 11 '21 at 23:49
  • I know why the error is occurring, but I want to be able to print it in the console – anon comp Aug 12 '21 at 00:40