Search code examples
angulartypescriptexceptiondisplaysweetalert

how to catch response and display the exception handle in back-end in angular


i am trying to catch the response and display in the browser when data is not found but i didn't got idea how to do it when response is in array.

In the postman response is in following way: [ { "responseStatus": "No record found!!", "responseCode": "0" } ] i tried to catch and display in the browser but not worked. i tried this way:

 if (response.responseCode == "0") {
        Swal.fire({
          icon:"warning",
          title: 'Data Not Exists!',
          confirmButtonColor: 'green',
        })
      }

How to catch response and display in browser when response given is in array form like :[ { "responseStatus": "No record found!!", "responseCode": "0" } ]

Thank you in advance


Solution

  • if (response.responseCode == "0") {

    What does the response look like?

    Is it this?

    response = [ 
      { "responseStatus": "No record found!!", "responseCode": "0" } 
    ];
    

    or this?

    response = { "responseStatus": "No record found!!", "responseCode": "0" };
    

    The first one is an array, so you will need to do response[0].responseCode. The [0] is to reach into the array and get the first element.

    You will of course need to add some logics to handle when the response returns the success object/array.