Permalink
Browse files

fixed; default array construction edge case

closes #1108
  • Loading branch information...
1 parent c7f248a commit 3fed499a76e700b295befad1fdac2a85f1d8ef10 @aheckmann aheckmann committed Sep 19, 2012
Showing with 22 additions and 1 deletion.
  1. +3 −1 lib/schema/documentarray.js
  2. +19 −0 test/model.field.selection.test.js
@@ -157,16 +157,18 @@ function scopePaths (array, fields, init) {
, keys = Object.keys(fields)
, i = keys.length
, selected = {}
+ , hasKeys
, key
while (i--) {
key = keys[i];
if (0 === key.indexOf(path)) {
+ hasKeys || (hasKeys = true);
selected[key.substring(path.length)] = fields[key];
}
}
- return selected;
+ return hasKeys && selected || undefined;
}
/*!
@@ -291,4 +291,23 @@ describe('model field selection', function(){
})
})
})
+
+ it('selecting an array of docs applies defaults properly (gh-1108)', function(done){
+ var db = start()
+ , M = db.model(modelName, collection)
+
+ var m = new M({ title: '1108', comments: [{body:'yay'}] });
+ m.comments[0].comments = undefined;
+ m.save(function (err, doc) {
+ assert.ifError(err);
+ M.findById(doc._id).select('comments').exec(function (err, found) {
+ db.close();
+ assert.ifError(err);
+ assert.ok(Array.isArray(found.comments));
+ assert.equal(1, found.comments.length);
+ assert.ok(Array.isArray(found.comments[0].comments));
+ done();
+ })
+ });
+ })
})

0 comments on commit 3fed499

Please sign in to comment.