in Education by
so i'm reviewing and practicing making rest api with node mongoose and express. I'm having problem making the post part of the api as I'm getting an error that I'm not sure how to fix. I'm kinda sure how to do the put and delete part. But if someone can show what that will look like. It will be 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 to our api!' }); }); //gets the all the pokemon app.get('/pokemon', function(req, res) { Pokemon.find(function(err, doc) { res.send(doc); }); }); //gets a specific pokemon app.get('/pokemon/:id', function(req, res) { Pokemon.findOne({ _id: req.params.id},function (err, doc) { res.send(doc); }) }) //create a 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 the pokemon app.put('/pokemon/:id', function(req, res) { }) app.delete('/pokemon/:id', function(req, res) { }); the error TypeError: Cannot read property 'pokeid' of undefined
   at /home/g62/pokemonsite/server/server.js:54:30
   at Layer.handle [as handle_request] (/home/g62/pokemonsite/server/node_modules/express/lib/router/layer.js:95:5)
   at next (/home/g62/pokemonsite/server/node_modules/express/lib/router/route.js:131:13)
   at Route.dispatch (/home/g62/pokemonsite/server/node_modules/express/lib/router/route.js:112:3)
   at Layer.handle [as handle_request] (/home/g62/pokemonsite/server/node_modules/express/lib/router/layer.js:95:5)
   at /home/g62/pokemonsite/server/node_modules/express/lib/router/index.js:277:22
   at Function.process_params (/home/g62/pokemonsite/server/node_modules/express/lib/router/index.js:330:12)
   at next (/home/g62/pokemonsite/server/node_modules/express/lib/router/index.js:271:10)
   at cors (/home/g62/pokemonsite/server/node_modules/cors/lib/index.js:179:7)
   at /home/g62/pokemonsite/server/node_modules/cors/lib/index.js:229:17
   at originCallback (/home/g62/pokemonsite/server/node_modules/cors/lib/index.js:218:15)
   at /home/g62/pokemonsite/server/node_modules/cors/lib/index.js:223:13
   at optionsCallback (/home/g62/pokemonsite/server/node_modules/cors/lib/index.js:204:9)
   at corsMiddleware (/home/g62/pokemonsite/server/node_modules/cors/lib/index.js:209:7)
   at Layer.handle [as handle_request] (/home/g62/pokemonsite/server/node_modules/express/lib/router/layer.js:95:5)
   at trim_prefix (/home/g62/pokemonsite/server/node_modules/express/lib/router/index.js:312:13) I'm been trying to fix it but not sure how. using postman for this JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
req.body Contains key-value pairs of data submitted in the request body. By default, it is undefined, and is populated when you use body-parsing middleware such as body-parser and multer. The following example shows how to use body-parsing middleware to populate req.body. var app = require('express')(); var bodyParser = require('body-parser'); var multer = require('multer'); // v1.0.5 var upload = multer(); // for parsing multipart/form-data app.use(bodyParser.json()); // for parsing application/json app.use(bodyParser.urlencoded({ extended: true })); // for 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 the link: http://expressjs.com/en/api.html#req.body

Related questions

0 votes
    What I'm trying to do here is use the 2 functions but I don't know how to use both of them ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    What I'm trying to do here is use the 2 functions but I don't know how to use both of them ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 4, 2022 in Education by JackTerrance
0 votes
    I have been looking at using the ssh2 module for sftp shipping of logs. However the cloud service hosting ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 18, 2022 in Education by JackTerrance
0 votes
    The napi_create_date function appeared in Node.js starting with 11.11.0 version. https://nodejs.org/api/n- ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 17, 2022 in Education by JackTerrance
0 votes
    I'm trying to debug my nodejs app using node-inspector. But Google Chrome doesn't show the code. ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 11, 2022 in Education by JackTerrance
0 votes
    Quick Summary of my question: Does Gulp 3.6.0 dest() handle glob-base the same way as 4.0.0? ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 19, 2022 in Education by JackTerrance
0 votes
    I'm used to v3 node botbuilder sdk so I have a middleware where I look at the dialog stack and ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 24, 2022 in Education by JackTerrance
0 votes
    I'm trying to use node to print the Serial from Arduino uno connected to USB I have a file script ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 30, 2022 in Education by JackTerrance
0 votes
    i'm trying to test with jest this route on my node microservice. At the initalisation of my microservice ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 26, 2022 in Education by JackTerrance
0 votes
    I've been putzing with trying to call the jobs.Insert bigquery rest api endpoint with node (the jobs. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 16, 2022 in Education by JackTerrance
0 votes
    What is a Node?...
asked Nov 20, 2020 in Education by Editorial Staff
0 votes
    I'm trying to include actual date and time to the certain message, however I'm unable to do it, ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 2, 2022 in Education by JackTerrance
0 votes
    axios.js throws CONNRESET error (certificate not found in request) but request package works for same p12 ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 2, 2022 in Education by JackTerrance
0 votes
    I have an application using Bull for a queue. Is there a parameter that I can pass it to set a ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 2, 2022 in Education by JackTerrance
0 votes
    How should I handle /_ah/start and /_ah/stop requests? Right now they're just getting 404s. Also, ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
...