Node.js Mongoose:自定义类型扩充模式
我想使用mongoose自定义类型的强大功能,但有一个问题我找不到解决方法。基本上,我们需要使用以下模式为日期保留时区Node.js Mongoose:自定义类型扩充模式,node.js,mongodb,mongoose,Node.js,Mongodb,Mongoose,我想使用mongoose自定义类型的强大功能,但有一个问题我找不到解决方法。基本上,我们需要使用以下模式为日期保留时区 { date: {type: Date}, //UTC date for ease of use zone: {type: String} //Initial timezone that allows to restore local date } 我已经用cast函数为标准日期字符串返回{date:,zone:}对象2014-10-24T14:00:00+050
{
date: {type: Date}, //UTC date for ease of use
zone: {type: String} //Initial timezone that allows to restore local date
}
我已经用cast
函数为标准日期字符串返回{date:,zone:}
对象2014-10-24T14:00:00+0500
(不确定这是否有效,所以欢迎任何建议)。
问题是给定以下代码
//Schema is extended with LocalDate already
var test = new Schema({
birthday: {type: Schema.Types.LocalDate}
});
var doc = new test({birthday: '2014-10-24T14:00:00+0500'});
doc.save(function(err){
//err is null here.
});
已插入数据库且具有_id的文档。但根本不保存生日路径
看起来好像从未调用过cast
。下面是它的定义
module.exports = function(mongoose){
var Schema = mongoose.Schema;
var Types = mongoose.Types;
var SchemaType = mongoose.SchemaType;
function LocalDate(){
SchemaType.call(this, key, options);
}
LocalDate.prototype.__proto__ = SchemaType.prototype;
//Here comes checkRequired, $conditionalHandlers etc. which seem to be irrelevant here
LocalDate.prototype.cast = function(val, scope, init){
console.log('casting LocalDate'); //Never logged
if (null === val || '' === val) return null;
if (_.isObject(val)){
return fromObject(val);
}
throw new SchemaType.CastError('LocalDate', val);
};
Schema.Types.LocalDate = LocalDate;
Types.LocalDate = LocalDate;
return LocalDate;
}
你知道这里有什么问题吗