使用mockgoose为mongoose(mongodb库为node.js)嘲笑填充方法是null

无法debugging模拟了填充字段设置属性的问题。 Yads mockgoose http://github.com/yads/Mockgoose fork解决了填充选项工作的错误,但是如果你指定了字段,它会为填充的属性返回一个空值。 我试着通过源代码查看,并通过debugging器,但不知道在哪里看。 我可以在debugging器中看到填充选项会触发一个调用来获取子元素 – 而且我看到调用返回正确的子字段的结果,但是当父元素最终返回时,它具有该子元素的属性元素设置为null。

查询:

Posts.findById(foo).populate('createdBy', {fname:1, lname:1}); 

用post.createdBy = null错误地返回一个post。 省略fame,lname的字段参数,以某种方式使它再次与post.createdBy返回完整的对象。

以下是代码的一些摘录 – 尽pipe我不确定那些是正确的地方看。

collections.js

 this.find = function (conditions, options, callback) { var results; var models = db[name]; if (!_.isEmpty(conditions)) { results = utils.findModelQuery(models, conditions); } else { results = utils.objectToArray(utils.cloneItems(models)); } results = filter.applyOptions(options, results); if (results.name === 'MongoError') { callback(results); } else { var result = { toArray: function (callback) { callback(null, results); } }; callback(null, result); } }; 

util.js中

 function cloneItems(items) { var clones = {}; for (var item in items) { clones[item] = cloneItem(items[item]); } return clones; } function cloneItem(item) { return _.cloneDeep(item, function(value) { // Do not clone items that are ObjectId objects as _.clone mangles them if (value instanceof ObjectId) { return new ObjectId(value.toString()); } }); } 

这是关于这个问题的一个谈话

https://github.com/mccormicka/Mockgoose/pull/90