【发布时间】:2012-12-19 17:46:41
【问题描述】:
我有以下代码 sn-ps 在项目中嵌入了注释
var CommentModel = new Schema({
text: {type: String, required: true},
}, {strict: true})
CommentModel.options.toJSON = { transform: function(doc, ret, options){
delete ret.__v;
delete ret._id;
}}
Comment = mongoose.model('Comment', CommentModel);
var ItemModel = new Schema({
name: {type: String, required: true},
comments: [ Comment ]
}, {strict: true})
Item = mongoose.model('Item', ItemModel);
Item.findOne({}, function (err, item) {
item.comments.forEach(function(o) {
console.log(o.toJSON)
})
})
但是,返回的结果对象数组似乎不是猫鼬对象,或者至少没有应用转换。我是否在某处遗漏了某些东西,或者猫鼬不支持这一点?
【问题讨论】:
-
我认为
toJSON选项是在架构上设置的,而不是在模型上。 -
这可能是我狡猾的命名约定。它在架构上设置。
-
哦,是的,呃,我应该仔细看看。
标签: javascript node.js mongoose