0

Here is my api:

getStudentAnswerById = async (student_id) => {
        var result = {};
        await fetch(`/api/getstudentanswer/${student_id}`)
            .then(response => response.json())
            .then(json => { result = json; });
        return result;
    }

enter image description here

If there is no data in the database against student_id, I gives Not Found, status 404 and program gets stops, how to handle this type of error in javascript?

Error is coming on the fetch as you can see in the picture,so it is not going to below condition to catch error. Thanks in advance

  • 1
    [Using Fetch - Checking that the fetch was successful](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#checking_that_the_fetch_was_successful) – Dave Anderson May 05 '21 at 04:23
  • But it is giving error in following line await fetch(`/api/getstudentanswer/${student_id}`) , and terminating the program, not coming to below line – Muhammad Ehsan Shaikh May 05 '21 at 05:22
  • Check the [Console](https://developer.mozilla.org/en-US/docs/Web/API/Console) for the actual error indicated by the code underlined in red. – Dave Anderson May 05 '21 at 11:53

0 Answers0