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())
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())
Share
Improve this question
edited Nov 27, 2018 at 20:41
user5734311
asked Nov 27, 2018 at 20:33
sherylsheryl
491 gold badge2 silver badges10 bronze badges
8
- 1 Is it "PUT" or "POST"? – user5734311 Commented Nov 27, 2018 at 20:42
-
can you post our postman request? If express handles JSON something like: (
curl -X PUT -H "Content-Type: application/json" -d '{"email":"value"}' "/api/v1/redflag/1"
) -v – dm03514 Commented Nov 27, 2018 at 20:42 -
redflags_table.map
That is the wrong way to find an element in an array. – epascarello Commented Nov 27, 2018 at 20:48 - Is it req.body or is it recordFound that is undefined? – epascarello Commented Nov 27, 2018 at 20:50
- it is PUT @ChrisG – sheryl Commented Nov 27, 2018 at 20:51
4 Answers
Reset to default 3Rearrage the order of your middleware.
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const router = require('./routes/index.js');
app.use(bodyParser.urlencoded({ extended: true }));
router.use(bodyParser.json())
app.use(router);
UPDATE 2021
Now body-parser
is deprecated, you can use express
:
import express from 'express'
const app = express()
app.use(express.json({limit: '20mb'}))
app.use(express.urlencoded({ extended: false, limit: '20mb' }))
export default app
If you are using Mongoose then it can be solved by fixing options .like this mongoose.set('useFindAndModify', false);
Hopefully your problem will be sorted .
You can fix this with these simple steps.
install body parser for content-type - application/x-www-form-urlencoded
1) npm i --save body-parser
2)go into your root file in your case it is index.js add these lines of code before routes.
//require body-parser
const bodyParser = require('body-parser')
app.use(bodyParser.urlencoded({ extended: true }))
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744179851a4561930.html
评论列表(0条)