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 'pokeid' of undefined <br> at /home/g62/pokemonsite/server/server.js:54:30 <br> at layer.handle [as handle_request] (/home/g62/pokemonsite/server/node_modules/express/lib/router/layer.js:95:5) <br> at next (/home/g62/pokemonsite/server/node_modules/express/lib/router/route.js:131:13) <br> at route.dispatch (/home/g62/pokemonsite/server/node_modules/express/lib/router/route.js:112:3) <br> at layer.handle [as handle_request] (/home/g62/pokemonsite/server/node_modules/express/lib/router/layer.js:95:5) <br> at /home/g62/pokemonsite/server/node_modules/express/lib/router/index.js:277:22 <br> at function.process_params (/home/g62/pokemonsite/server/node_modules/express/lib/router/index.js:330:12) <br> at next (/home/g62/pokemonsite/server/node_modules/express/lib/router/index.js:271:10) <br> at cors (/home/g62/pokemonsite/server/node_modules/cors/lib/index.js:179:7) <br> at /home/g62/pokemonsite/server/node_modules/cors/lib/index.js:229:17 <br> at origincallback (/home/g62/pokemonsite/server/node_modules/cors/lib/index.js:218:15) <br> at /home/g62/pokemonsite/server/node_modules/cors/lib/index.js:223:13 <br> at optionscallback (/home/g62/pokemonsite/server/node_modules/cors/lib/index.js:204:9) <br> at corsmiddleware (/home/g62/pokemonsite/server/node_modules/cors/lib/index.js:209:7) <br> at layer.handle [as handle_request] (/home/g62/pokemonsite/server/node_modules/express/lib/router/layer.js:95:5) <br> at trim_prefix (/home/g62/pokemonsite/server/node_modules/express/lib/router/index.js:312:13)
i'm been trying fix not sure how.
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
Post a Comment