2

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!

karvai
  • 2,417
  • 16
  • 31
  • Have a look at the [difference between `.then(…, …)` and `.then(…).catch(…)`](https://stackoverflow.com/q/24662289/1048572) as to why the rejection is not handled by your error callback. Regarding the error itself, "*TypeError: res.status(...).then is not a function*" is quite clear. – Bergi Jun 06 '20 at 19:41
  • Also logic doesn't make sense to even call `res.status()` before you do the remote request with axios – charlietfl Jun 06 '20 at 19:45

1 Answers1

1

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....');
      });
      ...
atopion
  • 36
  • 4
  • This actually removed the error. @charlietfl Gonna be a challenge passing in status for error I suppose considering it could be 4xx or 5xx unlike sucess when I was able to hard code 200. – karvai Jun 06 '20 at 20:14
  • @karvai You could use the status from the axios post error object or just send your own – charlietfl Jun 06 '20 at 20:16
  • @charlietfl Do you mean something along the lines of: }, error => { console.log('bombed....'); res.status(error.status).send('Error'); }); Will give it a go. Thanks. – karvai Jun 06 '20 at 20:24
  • 1
    @karvai Yes. I have not use axios for a very long time but assume the status property is as you are showing – charlietfl Jun 06 '20 at 20:27