html - insomnia -- Cannot read property 'post' of undefined -


var express = require('express'); var path = require('path'); var app = express(); var mongoose = require('mongoose'); var bodyparser = require('body-parser');  mongoose.promise = require('bluebird'); mongoose.connect("mongodb://makja:q1w2e3r4@ds111123.mlab.com:11123/makja" , { usemongoclient: true });  var db = mongoose.connection; db.once("open", function () {   console.log("db on!"); }); db.on("error", function (err) {   console.log("db error : " , err); });  var dataschema = mongoose.schema({   name:string,   count:number });  var postschema = mongoose.schema({   title : {type:string, required:true},   body : {type:string, required:true},   createdat : {type:date, default:date.now},   updatedat : date }); var post = mongoose.model('post',postschema);  app.get('/posts', function(req , res){   post.find({}, function(err,posts){     if (err) return res.json({success:false, message:err});     res.json({success:true, data:posts});   }); }); app.post('/posts', function(req , res){   post.create(req.body.post, function(err,post){     if (err) return res.json({success:false, message:err});     res.json({success:true, data:post});   }); });  var data = mongoose.model('data',dataschema); data.findone({name:"mydata"},function(err,data){   if (err) return console.log("data error",err);   if (!data) {     data.create({name:"mydata",count:0},function(err,data){       if (err) return console.log("data error:",err);       console.log("counter initalized:",data);     });   } });  app.set("view engine",'ejs'); app.use(express.static(path.join(__dirname,'public'))); app.use(bodyparser.json());  app.get('/',function(req , res) {   data.findone({name:"mydata"}, function(err,data){     if(err) return console.log("data error: ",err);     data.count++;     data.save(function(err){       if(err) return console.log("data error: ",err);       res.render('my_first_ejs',data);     });   }); });  app.get('/',function (req,res) {   res.render('my_first_ejs'); });  app.listen(8000, function(){`enter code here`   console.log('server on!'); }); 

cmd error -------------------------------

typeerror: cannot read property 'post' of undefined     @ c:\nodejs\index.js:38:23     @ layer.handle [as handle_request] (c:\nodejs\node_modules\express\lib\router\layer.js:95:5)     @ next (c:\nodejs\node_modules\express\lib\router\route.js:137:13)     @ route.dispatch (c:\nodejs\node_modules\express\lib\router\route.js:112:3)     @ layer.handle [as handle_request] (c:\nodejs\node_modules\express\lib\router\layer.js:95:5)     @ c:\nodejs\node_modules\express\lib\router\index.js:281:22     @ function.process_params (c:\nodejs\node_modules\express\lib\router\index.js:335:12)     @ next (c:\nodejs\node_modules\express\lib\router\index.js:275:10)     @ expressinit (c:\nodejs\node_modules\express\lib\middleware\init.js:40:5)     @ layer.handle [as handle_request] (c:\nodejs\node_modules\express\lib\router\layer.js:95:5)     @ trim_prefix (c:\nodejs\node_modules\express\lib\router\index.js:317:13)     @ c:\nodejs\node_modules\express\lib\router\index.js:284:7     @ function.process_params (c:\nodejs\node_modules\express\lib\router\index.js:335:12)     @ next (c:\nodejs\node_modules\express\lib\router\index.js:275:10)     @ query (c:\nodejs\node_modules\express\lib\middleware\query.js:44:5)     @ layer.handle [as handle_request] (c:\nodejs\node_modules\express\lib\router\layer.js:95:5) 

make sure bodyparser parsing body , attaching body req before post getting handled.

--> app.use(bodyparser.json());

var app = require('express')(); var bodyparser = require('body-parser'); app.post('/', (req, res) =>{     console.log(req.body); //undefined     res.json(req.body).status(200); }) app.use(bodyparser.json()); app.listen(3000);  ====  var app = require('express')(); var bodyparser = require('body-parser'); app.use(bodyparser.json()); app.post('/', (req, res) =>{     console.log(req.body); //attaches correctly     res.json(req.body).status(200); })  app.listen(3000); 

Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -