2015-06-24 102 views
0

我試圖改寫貓鼬模型的制定者,因爲我要創造我自己的Date對象,但檢查documentation,我也跟隨,但二傳手仍是不叫:貓鼬架構二傳手覆蓋功能是不是叫

var mongoose = require('mongoose'); 

function strToDate(dateStr){ 
    var dateStr = dateStr.split('/'); 
    console.log('parsed a date!'); 
    return new Date(dateStr[0],dateStr[1],dateStr[2],dateStr[3],dateStr[4], dataStr[5]); 
} 

var infoSchema = new mongoose.Schema({ 
    code: {type : String, require: true}, 
    valve: {type : Number, require: true}, 
    dataTime: {type: Date, require: true, set: strToDate}, 
    status: {type: Boolean, require: true, default: false }, 
    elapsedTime: 0 
}); 


var dataLogSchema = new mongoose.Schema({ 
    boardId: {type: String, require: true, unique: true}, 
    logInfo:[infoSchema] 
}); 



module.exports = mongoose.model('DataLog', dataLogSchema); 

有什麼不對?我使用的是貓鼬4.0.5

回答

0

的問題是,你有一個錯字在你strToDate功能是造成該函數拋出一個ReferenceError異常(其中貓鼬顯然只是吃)。代替dataStr[5]應該是dateStr[5]

所以剛纔那個函數的最後一行更改爲:

return new Date(dateStr[0],dateStr[1],dateStr[2],dateStr[3],dateStr[4],dateStr[5]); 
0

我試圖與貓鼬這個例子4.0.6

var mongoose = require('mongoose'); 
var Schema = mongoose.Schema; 

var InfoSchema = new Schema({ 
    dataTime: {type: Date, require: true, set: strToDate} 
}); 

function strToDate(stringDate) { 
    var sDate = stringDate.split('/'); 
    return new Date(sDate[0], sDate[1], sDate[2], sDate[3], sDate[4], sDate[5]); 
} 

var Info = mongoose.model('Info', InfoSchema); 

var info = new Info(); 
info.dataTime = '1995/11/17/3/24/0'; 
console.log(info.dataTime); //Sun Dec 17 1995 03:24:00 GMT-0400 (BOT)