Skip to content

Commit

Permalink
fixed; default array construction edge case
Browse files Browse the repository at this point in the history
closes #1108
  • Loading branch information
aheckmann committed Sep 19, 2012
1 parent dbda275 commit 3bd5119
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/schema/documentarray.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
19 changes: 19 additions & 0 deletions test/model.field.selection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,4 +289,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 3bd5119

Please sign in to comment.