while using node.js i am not able to insert a data to database ,i am using express.js ,mongoose but getting error which i mentioned in code as comment
Blockquote error is Blockquote TypeError: Cannot read property 'name' of undefined at insert (E:\node\website\controller\signup.js:5:25) at Layer.handle [as handle_request] (E:\node\website\node_modules\express\lib\router\layer.js:95:5) at next (E:\node\website\node_modules\express\lib\router\route.js:137:13) at Route.dispatch (E:\node\website\node_modules\express\lib\router\route.js:112:3) at Layer.handle [as handle_request] (E:\node\website\node_modules\express\lib\router\layer.js:95:5) at E:\node\website\node_modules\express\lib\router\index.js:281:22 at Function.process_params (E:\node\website\node_modules\express\lib\router\index.js:335:12) at next (E:\node\website\node_modules\express\lib\router\index.js:275:10) at Function.handle (E:\node\website\node_modules\express\lib\router\index.js:174:3) at router (E:\node\website\node_modules\express\lib\router\index.js:47:12) at Layer.handle [as handle_request] (E:\node\website\node_modules\express\lib\router\layer.js:95:5) at trim_prefix (E:\node\website\node_modules\express\lib\router\index.js:317:13) at E:\node\website\node_modules\express\lib\router\index.js:284:7 at Function.process_params (E:\node\website\node_modules\express\lib\router\index.js:335:12) at next (E:\node\website\node_modules\express\lib\router\index.js:275:10) at expressInit (E:\node\website\node_modules\express\lib\middleware\init.js:40:5) at Layer.handle [as handle_request] (E:\node\website\node_modules\express\lib\router\layer.js:95:5) at trim_prefix (E:\node\website\node_modules\express\lib\router\index.js:317:13) at E:\node\website\node_modules\express\lib\router\index.js:284:7 at Function.process_params (E:\node\website\node_modules\express\lib\router\index.js:335:12) at next (E:\node\website\node_modules\express\lib\router\index.js:275:10) at query (E:\node\website\node_modules\express\lib\middleware\query.js:45:5) Blockquote //controller file named as signup.js
var express=require('express');
var post1=require('../model/singup')
var router=express.Router();
var insert=(req,res)=>{
var name1= req.body.name;// error:name is not defined
var email1= req.body.email;//error:email is not defined
var password1= req.body.password;//error:password is not defined
post1.create({
name: name1,email:email1,password:password1
},(err,result)=>{
if(err){res.json({message:'error'
})
}
else{res.json({message:'sucessful',result:result
})
}
})
}
router.post('/signup',insert);
router.get('/h',(req,res)=>{
res.json({
message:'insert'
})
console.log("insert");
})
module.exports=router;
Blockquote //model file named as signup.js
require('../db');
var mongoose=require('mongoose');
mongoose.Promise=require('bluebird');
var schema= new mongoose.Schema({
name:{type:String,required:true},
email:{type:String,required:true},
password:{type:String,required:true},
},
{timestamp:true});
module.exports=mongoose.model('post1',schema);
Blockquote //db.js file for database connection
var mongose=require('mongoose');
mongose.set('useNewUrlParser', 'true');
mongose.set('useCreateIndex', 'true');
mongose.set('useFindAndModify', 'true');
mongose.connect("mongodb://127.0.0.1:27017/website");
var db=mongose.connection;
db.on('error',console.error.bind(console,'connection error'));
db.once('open',(req,res)=>{
console.log("connected with db!!!");
})
Blockquote //index.js
var app=require('./server')
var signup=require('./controller/signup')
var session=require('express-session')
app.set('viewengin','ejs')
app.set('useNewUrlParser', 'true')
app.set('useUnifiedTopology', 'true')
app.use('/user',signup)
app.use(session({
secret:'keyboard cat ',
resave:false,
cookies:{
secrue:true
},
}))
app.all('*',(req,res)=>{
return res.json({status: 205,message: 'not found'});
})
var server=app.get('/',(req,res)=>{
req.end()
});