UnhandledPromiseRejectionWarning when nesting rest calls

Viewed 99

I have the following Rest call where The second rest call needs data from the first call as part of its request. Thus I have nested them as follows. New to JS/React and as far as I have tried to read up, this nesting style doesn't seem to be frowned upon. Advice if otherwise.

The following works based on the fact that the value 'Sending Email Worked!' prints (from console log below). Also, received an email due to this rest call as expected.

My Issue is the following error message. Why do I get this error? I do not get this if I only make a single rest call thus believe issue is with my nesting. I certainly have catch blocks as follows. Could I get some idea as to why this is happening and how to prevent it? Thanks.

exports.getData = (req, res) => {
  // 1st rest call  
  axios.post(urls.env.endpoint1, getPostBody1(req.body.code))
    .then((output) => {
      // needed this name value from the first call to be able to pass in the next rest call below. 
      const name = output.data.name_response_data_list[0].short_name;
      console.log(name);
      res.status(200).then(
        // 2nd rest call
        axios.post(urls.env.endpoint2, getPostBody2(name, req.body.email_address))
          .then(() => {
            console.log('Sending Email Worked!');
            res.status(200).send('Success');
          }, () => {
            console.log('bombed....');
          })
      );
    }, (error) => {
      console.log(error);
    });
};

Error Message:

[server:watch] (node:77802) UnhandledPromiseRejectionWarning: TypeError: res.status(...).then is not a function [server:watch]
at axios.post.then (/Users/name/projects/front/src/server/controllers/Controller1.js:77:23) [server:watch] at process._tickCallback (internal/process/next_tick.js:68:7) [server:watch] (node:77802) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1) [server:watch] (node:77802) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code. [server:watch] Sending Email Worked!

1 Answers

The reason for this error is probably that the function res.status() does not return a Promise and therefore its return value does not have a .then() function.

Furthermore, if I am interpreting the code correctly, for every request, you are sending two requests to an endpoint and reply "Success" if it works with status code 200. However, your code sets this status code twice (once after each endpoint request). I don't think that this has any advantage, however, I don't know the rest of your code.

If you only want to send the status 200 after both requests are finished (as would make sense), you could simply delete the res.status(200).then( and change it to

      ...
      const name = output.data.name_response_data_list[0].short_name;
      console.log(name);
      // 2nd rest call
      axios.post(urls.env.endpoint2, getPostBody2(name, req.body.email_address))
      .then(() => {
        console.log('Sending Email Worked!');
        res.status(200).send('Success');
      }, () => {
        console.log('bombed....');
      });
      ...
Related