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!