Hi all,
I have a route of type ‘post’, that is receiving some info from the front-end, as shown below:
const router = require("express").Router();
const GameModel = require("../models/game.model");
const UsernameController = require("../controllers/username.controller");
router.post("/username", async (req, res) => {
try {
const cookie = req.cookies;
const {userName} = req.body;
let allGames = await new UsernameController().getGames(userName);
console.log(allGames[0].games)
return res.sendStatus(200)
} catch (err) {
console.log(err);
res.status(422).send(err);
};
});
module.exports = router;
I need to use the destructured {userName} = req.body
in another middleware.js file. So I’m wondering how I can export the {userName} received from the fornt-end to the middleware.js file.
The middleware.js file is as shown below:
const AuthController = require("../controllers/auth.controller");
const UsernameController = require("../controllers/username.controller");
const usernameRouter = require('../routes/username.route')
module.exports = async (req, res, next) => {
let userName = usernameRouter.userName
const userGamesArray = await new UsernameController().getGames(userName)
req.userGamesArray = userGamesArray;
next();
};
When I console.log the userName variable in the middleware file, it responds with undefined; which means I’m importing the variable wrongly from the route.js file.
Kindly assist me with importing the variable from the route.js file to the middleware.js file.