I'm writing a code in express js that will post data and create a recorded. The record takes 1-3 seconds to get created, not sure of how long it will take. Is there a way that I can redirect to that record after that is created?
Here is my current code.
router.post("/recipePost", async (req, res) => {
var body = req.body;
var axios = require("axios");
var data = JSON.stringify({
"name":"john"
});
var config = {
method: "post",
url:
"https://myEndpoint.com",
headers: {
"Content-Type": "application/json",
},
data: data,
};
try {
var resData = await axios(config);
navigateToRec(resData.data.response.meta.id);
} catch (err) {
console.log("err");
}
});
and my navigate method is as below.
navigateToRec = async (recId) => {
console.log("recId" + recId);
const resp = await axios.get(
`https://myEndpoint.com/${recId}`
);
try {
res.send({ redirect: "/recipeDetail/" + resData.data.response.meta.id });
} catch (err) {
this.navigateToRec(rec);
}
};
Basically trying to call the same method till the record creation is a success. But here console.log("recId" + recId); is printing the Id created. Please let me know where am I going wrong and how can I fix this.
Here is the error
(node:83576) UnhandledPromiseRejectionWarning: Error: Request failed with status code 404 at createError (/Users/rkeerthi/Desktop/Node js/yext-site/node_modules/axios/lib/core/createError.js:16:15) at settle (/Users/rkeerthi/Desktop/Node js/yext-site/node_modules/axios/lib/core/settle.js:17:12) at IncomingMessage.handleStreamEnd (/Users/rkeerthi/Desktop/Node js/yext-site/node_modules/axios/lib/adapters/http.js:293:11) at IncomingMessage.emit (events.js:412:35) at endReadableNT (internal/streams/readable.js:1317:12) at processTicksAndRejections (internal/process/task_queues.js:82:21) (Use
node --trace-warnings ...to show where the warning was created) (node:83576) 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(). To terminate the node process on unhandled promise rejection, use the CLI flag--unhandled-rejections=strict(see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2) (node:83576) [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.