mongodb - restapi using mongoose and node.js on post,put, and delete -


so i'm reviewing , practicing making rest api node mongoose , express. i'm having problem making post part of api i'm getting error i'm not sure how fix. i'm kinda sure how put , delete part. if can show like. great.

mongoose.connect('mongodb://localhost:27017/pokemon');  var pokemonschema = {     pokeid: number,     name:string,     type:string,     weight:string,     height: string,     date: { type: date, default: date.now },     summary: string  }  var pokemon = mongoose.model('pokemon', pokemonschema);  var app = express(); app.use(cors());  app.get('/api', function(req, res) {     res.json({ message: 'hooray! welcome our api!' }); });  //gets pokemon app.get('/pokemon', function(req, res) {     pokemon.find(function(err, doc) {         res.send(doc);     }); });  //gets specific pokemon app.get('/pokemon/:id', function(req, res) {     pokemon.findone({ _id: req.params.id},function (err, doc) {         res.send(doc);     }) })   //create new pokemon app.post('/pokemon', function(req, res) {      var pokemon = new pokemon();      pokemon.pokeid = req.body.pokeid;     pokemon.name = req.body.name;     pokemon.type = req.body.type;     pokemon.weight = req.body.weight;     pokemon.height = req.body.height;     pokemon.summary = req.body.summary;      //save pokemon     pokemon.save(function(err) {         if (err)             res.send(err);         res.json({ message: 'pokemon created'});     });   })  //update pokemon  app.put('/pokemon/:id', function(req, res) {   })  app.delete('/pokemon/:id', function(req, res) {  }); 

the error

typeerror: cannot read property &#39;pokeid&#39; of undefined <br> &nbsp; &nbsp;at /home/g62/pokemonsite/server/server.js:54:30 <br> &nbsp; &nbsp;at layer.handle [as handle_request] (/home/g62/pokemonsite/server/node_modules/express/lib/router/layer.js:95:5) <br> &nbsp; &nbsp;at next (/home/g62/pokemonsite/server/node_modules/express/lib/router/route.js:131:13) <br> &nbsp; &nbsp;at route.dispatch (/home/g62/pokemonsite/server/node_modules/express/lib/router/route.js:112:3) <br> &nbsp; &nbsp;at layer.handle [as handle_request] (/home/g62/pokemonsite/server/node_modules/express/lib/router/layer.js:95:5) <br> &nbsp; &nbsp;at /home/g62/pokemonsite/server/node_modules/express/lib/router/index.js:277:22 <br> &nbsp; &nbsp;at function.process_params (/home/g62/pokemonsite/server/node_modules/express/lib/router/index.js:330:12) <br> &nbsp; &nbsp;at next (/home/g62/pokemonsite/server/node_modules/express/lib/router/index.js:271:10) <br> &nbsp; &nbsp;at cors (/home/g62/pokemonsite/server/node_modules/cors/lib/index.js:179:7) <br> &nbsp; &nbsp;at /home/g62/pokemonsite/server/node_modules/cors/lib/index.js:229:17 <br> &nbsp; &nbsp;at origincallback (/home/g62/pokemonsite/server/node_modules/cors/lib/index.js:218:15) <br> &nbsp; &nbsp;at /home/g62/pokemonsite/server/node_modules/cors/lib/index.js:223:13 <br> &nbsp; &nbsp;at optionscallback (/home/g62/pokemonsite/server/node_modules/cors/lib/index.js:204:9) <br> &nbsp; &nbsp;at corsmiddleware (/home/g62/pokemonsite/server/node_modules/cors/lib/index.js:209:7) <br> &nbsp; &nbsp;at layer.handle [as handle_request] (/home/g62/pokemonsite/server/node_modules/express/lib/router/layer.js:95:5) <br> &nbsp; &nbsp;at trim_prefix (/home/g62/pokemonsite/server/node_modules/express/lib/router/index.js:312:13) 

i'm been trying fix not sure how.

using postman enter image description here

req.body

contains key-value pairs of data submitted in request body. default, undefined, , populated when use body-parsing middleware such body-parser , multer.

the following example shows how use body-parsing middleware populate req.body.

var app = require('express')(); var bodyparser = require('body-parser'); var multer = require('multer'); // v1.0.5 var upload = multer(); // parsing multipart/form-data  app.use(bodyparser.json()); // parsing application/json app.use(bodyparser.urlencoded({ extended: true })); // parsing application/x-www-form-urlencoded  app.post('/profile', upload.array(), function (req, res, next) {   console.log(req.body);   res.json(req.body); }); 

please read link: http://expressjs.com/en/api.html#req.body


Comments