Issue
I have declared this method here
exports.postRedisValue = function(req,res) {
let keyRedis = req.body.key;
let valueRedis = req.body.value;
console.log(keyRedis); //throws undefined
if(keyRedis && valueRedis){
client.setex(
keyRedis,3600,valueRedis);
res.send(`Key and value are now set key is ${keyRedis} and value ${valueRedis}`);
}else{
res.send("Missing key or value");
}
}
When i am trying to create a key and a value in redis through a form
In routes.js I have declared the route
router.post('/createKeyValue',redisController.postRedisValue);
In the server.js I have declared starting point
const redisRoute = require('../routes/routes');
const app = express();
const PORT = 3001;
app.use(express.json());
app.use(express.urlencoded());
app.use("/redisLogic",redisRoute);
app.listen(PORT,() =>{
console.log("SERVER r");
})
But it keeps throwing undefined variable when trying to send a post request to /redisLogic/createKeyValue. What am i missing ?
The request that is being sent
Solution
Seems that you have not made use of body-parser
npm package in your application.
Try using body-parser
npm package
Install it with.
npm install --save body-parser
And inside your node app, use it with
const bodyParser = require('body-parser');
app.use(bodyParser);
Answered By – Nitheesh
This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0