经过一些调试后,我想出了这段代码:
var trackSchema = new mongoose.Schema({
title: String,
playedAtTimestamps: Array
});
trackSchema.post('init', function(track) {
track.playedAtTimestamps = track.playedAtTimestamps || [];
});
因此,代码中的其他任何位置,我知道track.playedAtTimestamps
是Array
。但我觉得我的post-init回调对我的Schema定义是多余的。
有更好的做法吗?我们应该以某种方式修改Mongoose的行为吗?
由于
答案 0 :(得分:0)
Gotcha:facepalm
我在做
Track.findOne({})
.select('title') // Here is the mistake, I was not fetching playedAtTimestamps
.exec(function(err, track){
console.log(track);
track.incCounterSync((new Date()).getTime()); // Here is the error
track.save(function(err, track){
console.log(track);
});
});