2015-11-08 99 views
1

以下的服用有助於節省:無法使用貓鼬

1. https://stackoverflow.com/a/30164855/2168120

2. mongodb auto-increment

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! 
    }); 
}); 

我無法將這個新的記錄保存到用戶表。

+0

什麼錯誤? – yarons

+0

沒有錯誤,但新的記錄沒有保存 – liza

+0

你是否達到'console.log(newuser.get('_ id'));'line? – yarons

回答

1

感謝this

  1. 使用貓鼬,自動遞增我可以創建一個自動遞增_id。

溶液:

var mongoose = require('mongoose'); 
var autoIncrement = require('mongoose-auto-increment'); 
var connection = mongoose.createConnection("mongodb://localhost/dfp", function(err){ 
    if(err){ 
    console.log(err); 
    }else{ 
    console.log("connected to mongodb from app"); 
    } 
}); 
autoIncrement.initialize(connection); 
/////// 
var userSchema = mongoose.Schema({email: String, fname: String, lname: String, pwd: String, gender: String, 
           mobile: String, dob: { type: Date, default: Date.now }, ip : String, doj: { type: Date, default: Date.now } 
           },{ collection : 'user' }); 
userSchema.plugin(autoIncrement.plugin, 'user'); 
var user = connection.model('user',userSchema); 
//////// 
  • 問題是在對於迴路;設置字段。
  • 解決方案:

    app.post('/save', function(req, res, next){ 
        var data = req.body; 
        var dob = new Date(data['dob']).toISOString(); 
        var doj = new Date().toISOString(); 
    
        var newuser = new user(); 
        for(var i in data){ 
        if(i != dob) { newuser.set(i, data[i]); } 
        } 
        newuser.dob = dob; newuser.doj = doj; 
    
        newuser.save(function(error, users){ 
        if (error) { next(); } 
        // Return ID of element in response. 
        console.log(users._id); 
        }); 
    
    });