Skip to content

Commit

Permalink
populate; fix saving populated arrays
Browse files Browse the repository at this point in the history
when null or ObjectId was added

relates to #570
  • Loading branch information
aheckmann committed Feb 3, 2013
1 parent ddcab4a commit 4bed277
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
6 changes: 4 additions & 2 deletions lib/types/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -568,9 +568,11 @@ MongooseArray.prototype.set = function set (i, val) {
*/

MongooseArray.prototype.toObject = function (options) {
if (options && options.depopulate && this[0] instanceof Document) {
if (options && options.depopulate) {
return this.map(function (doc) {
return doc._id;
return doc && doc._id // check for documents
? doc._id
: doc
});
}

Expand Down
14 changes: 13 additions & 1 deletion test/model.populate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2089,8 +2089,20 @@ describe('model: populate:', function(){
doc.fans.addToSet(_id);
assert.equal(doc.fans[7], _id);

done();
doc.save(function (err) {
assert.ifError(err);
B.findById(b1).exec(function (err, doc) {
assert.ifError(err);
assert.equal(8, doc.fans.length);
assert.equal(doc.fans[0], user8.id);
assert.equal(doc.fans[5], user7.id);
assert.equal(doc.fans[6], null);
assert.equal(doc.fans[7], String(_id));
done();
})
})
})
// TODO test setting _id directly
// TODO test with Buffer, Number, String _ids too
})
})
Expand Down

0 comments on commit 4bed277

Please sign in to comment.