刚开始使用mongodb并尝试实现插入集合,但我很挣扎。
user_model.js
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var userSchema = new Schema({
name:String,
email:String,
password:String,
phone:Number,
_enabled:Boolean
});
module.exports = mongoose.model('User', userSchema);
路由/ index.js
var User = require('../models/user_model.js');
var mongoose = require('mongoose');
mongoose.createConnection('mongodb://localhost:27017/test',function(err){
console.log(err);
});
router.get('/', function(req, res, next) {
var new_user = new User({
name:'hjhj',
email: 'jjjjhj',
password: 'jhjhj',
phone: '7778',
_enabled:false
});
new_user.save(function(err,User){
console.log("insdie");
if(err) return res.json(err);
console.log("After insert",User);
});
});
});
没有错误,但new_user.save(function(err){}
没有被触发,但是如果发生验证错误,则会触发错误回调,并显示正确的错误消息。