1
以下的服用有助於節省:無法使用貓鼬
1. https://stackoverflow.com/a/30164855/2168120
app.js:
//some codes
var mongoose = require('mongoose');
var connection = mongoose.createConnection("mongodb://localhost/dfp", function(err){
if(err){
console.log(err);
}else{
console.log("connected to mongodb from app");
}
});
模式:
var userSchema = mongoose.Schema({email: String, fname: String, lname: String, password: String, gender: String,
mobile: String, dob:{ type: Date, default: Date.now }, ip: String, doj:{ type: Date, default: Date.now }
},{ collection : 'user' });
var user = mongoose.model('user',userSchema);
var CounterSchema = mongoose.Schema({
_id: {type: String, required: true},
seq: { type: Number, default: 0 }
});
var counters = mongoose.model('counters', CounterSchema);
app.post():
app.post('/save', function(req, res, next){
var data = req.body;
var newuser = new user();
var dob = new Date(data['dob']);
var doj = new Date();
for(var i in data){
if(i != dob){
newuser.i = data[i];
}
}
newuser.dob = dob;
counters.findByIdAndUpdate({_id: 'userid'}, {$inc: { seq: 1} }, function(error, counter) {
if(error) {
return next(error); //https://stackoverflow.com/a/30164855/2168120
}
newuser._id = counter.seq;
next();
});
console.log(newuser.get('_id'));
newuser.save(function (err) {
if (err) {
return handleError(err);
}
// saved!
});
});
我無法將這個新的記錄保存到用戶表。
什麼錯誤? – yarons
沒有錯誤,但新的記錄沒有保存 – liza
你是否達到'console.log(newuser.get('_ id'));'line? – yarons