I’m trying to pass in some data I’ve received from my POST request to be utilized inside my GET request as shown below:
app.post('/genre/non-fiction', (req, res, next) => {
let received_data = req.body.myData;
});
app.get('/genre/non-fiction/:id/read', (req, res) => {
res.send("Hello World");
});
I’ve found this solution from this article that explains how to pass variables from one middleware to another using next()
, and so I tried something like this:
app.post('/genre/non-fiction', (req, res, next) => {
let received_data = req.body.myData;
req.pass_data = received_data;
next();
});
// I've also tried passing the "recieved_data" variable into the middleFunction, but that doesn't do anything either.
function middleFunction(req, res, next) {
console.log(req.pass_data);
next();
}
app.get('/genre/non-fiction/:id/read', middleFunction, (req, res) => {
res.send(req.pass_data);
});
When attempting to test it out, nothing shows up on the page. I’ve also console.logged the pass_data
variable inside the GET request, but it returns undefined
.
Any help would be greatly appreciated!