[英]Save POST data To MongoDB: Node.js
I'am new with node.js.I Need to save data from HTML page use - form (user name, password, email etc)in Mongodb. 我是node.js的新手,我需要保存HTML页面使用中的数据-Mongodb中的表单(用户名,密码,电子邮件等)。 I have this code:
我有以下代码:
var Schema = new mongoose.Schema({
_id : String,
name: String,
age : Number
});
var user = mongoose.model('emp', Schema);
app.post('/new', function(req, res){
new user({
_id : req.body.email,
name: req.body.name,
age : req.body.age
}).save(function(err, doc){
if(err) res.json(err);
else res.send('Successfully inserted!');
});
});
<form action="/new" method="POST">
<label for="email">Email: </label>
<input type="email" name="email" /><br />
<label for="name">Name: </label>
<input type="text" name="name" /><br />
<label for="age">Age: </label>
<input type="number" name="age" /><br />
<input type="submit"/>
When I run server. 当我运行服务器。 Trying to save some data.
尝试保存一些数据。 I see message "Cannot read property 'email' of undefined" "TypeError: Cannot read property 'email' of undefined" Somebody help me please with this misunderstood.
我看到消息“无法读取未定义的属性'email'”“ TypeError:无法读取未定义的属性'email'”有人帮助我解决了这一误解。 Thans a lot!
非常感谢!
Also you don't need to define _id in your schema, it gets automatically created. 另外,您无需在架构中定义_id,它会自动创建。 Maybe you meant to define email : String so it matches your req.body.email
也许您打算定义email:String,以便它与您的req.body.email匹配
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.