节点JS | TypeError:无法读取未定义的属性'first_name'

我刚开始使用MEAN应用程序,并且在将数据添加到数据库时遇到了问题。 所以请帮我find解决scheme。

这是应用程序中的根文件,入口点

//Importing modules var express=require('express'); var mongoose=require('mongoose'); var bodyparser=require('body-parser'); var cors=require('cors'); var path=require('path'); var app=express(); const route=require('./routes/route'); //Connect to mongoDb mongoose.connect('mongodb://localhost:27017/contactlist'); //on connection mongoose.connection.on('connected',()=>{ console.log("Successfully established a connection to mongodb Database ") }); //on error mongoose.connection.on('error',(err)=>{ if(err){ console.log("Failed to established a connection "+err); } }); const port=3000; //For routing app.use('/api',route); //Adding middleware -cors app.use(cors()); //body-parser app.use(bodyparser.json()); //Static Files app.use(express.static(path.join(__dirname,'public'))); //port no app.get('/',(req,res)=>{ res.send('Foobar'); }); app.listen(port,()=>{ console.log("Server started listening to port "+port); }) 

而这个我的路线文件,

 const express = require('express'); const router = express.Router(); // fetching the schema const Contact = require('../Models/contacts'); //Retriving the contacts router.get('/contacts', (req,res,next)=>{ Contact.find(function(err,contacts){ // Sending to client in json format res.json(contacts); }); }); // Adding Contacts router.post('/contact', (req,res,next)=>{ let newContact = new Contact({ first_name : req.body.first_name, last_name : req.body.last_name, phone : req.body.phone }); newContact.save((err,contact)=>{ if(err){ res.json({msg: "Failed to add contact."}); }else{ res.json({msg:"Contact added sucessfully"}); } }); }); //Deleteing contact router.delete('/contact/id',(req,res,next)=>{ Contact.remove({_id:req.params.id},function(err,result){ if(err){ res.json(err); }else{ res.json(result); } }) }); module.exports=router; 

现在,我试图用邮递员添加一些logging在数据库(Mongo DB),但它抛出一个错误,说:“TypeError:不能读属性'first_name'未定义,在router.post(C:\平均应用程序\ ContactList \路由\ route.js:16:25)”

在邮递员,为标题,我使用“内容types:应用程序/ JSON”和原始的身体,我添加像这样的JSON数据,

 { "first_name" : "Siddhesh", "last_name" : "Mishra", "phone" : "9594106324" } 

这里是我创build模式的代码

 const mongoose=require('mongoose'); const ContactSchema = mongoose.Schema({ first_name:{ type:String, required:true }, last_name:{ type:String, required:true }, phone:{ type:String, required:true } }); const Contact=module.exports=mongoose.model('Contact',ContactSchema); 

谢谢。

你将不得不移动pathuse上面的body-parser ,应该工作

 //body-parser app.use(bodyparser.json()); //For routing app.use('/api',route); 

从Express API v4.x

app.use([path,] callback [,callback …])

中间件function是按顺序执行的,因此中间件包含的顺序非常重要。

body-parser需要在路由之前:

 //Importing modules var express = require('express'); var mongoose = require('mongoose'); var bodyparser = require('body-parser'); var cors = require('cors'); var path = require('path'); var app = express(); const route = require('./routes/route'); //Connect to mongoDb mongoose.connect('mongodb://localhost:27017/contactlist'); //on connection mongoose.connection.on('connected', () => { console.log("Successfully established a connection to mongodb Database ") }); //on error mongoose.connection.on('error', (err) => { if (err) { console.log("Failed to established a connection " + err); } }); const port = 3000; //body-parser app.use(bodyparser.json()); // here //Adding middleware -cors app.use(cors()); //Static Files app.use(express.static(path.join(__dirname, 'public'))); // For routing app.use('/api', route); app.get('/', (req, res) => { res.send('Foobar'); }); app.listen(port, () => { console.log("Server started listening to port " + port); }) 

为了始终保持安全,你的路由应该总是在所有中间件之后。