I'm still trying to understand routing in node.js, Other routes like route.get(all) and single id are working perfectly, but "router.post" is giving an error in postman such as "TypeError: Cannot read property email of undefined";
For the index.js
const express = require('express');
const redflags_table = require('../db/redflags_table');
const router = express.Router();
router.put('/api/v1/redflag/:id', (req, res) => {
const id = parseInt(req.params.id, 10);
let recordFound;
let itemIndex;
redflags_table.map((record, index) => {
if (record.id === id) {
recordFound = record;
itemIndex = index;
}
});
if (!recordFound) {
return res.status(404).send({
success: 'false',
message: 'todo not found',
});
}
if (!req.body.email) {
return res.status(400).send({
success: 'false',
message: 'title is required',
});
}
const updatedRedflag = {
id: recordFound.id,
email: req.body.email || recordFound.email
};
redflags_table.splice(itemIndex, 1, updatedRedflag);
return res.status(201).send({
success: 'true',
message: 'todo added successfully',
updatedRedflag,
});
});
The app.js:
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const router = require('./routes/index.js');
app.use(router);
app.use(bodyParser.urlencoded({ extended: true }));
router.use(bodyParser.json())