Skip to content

Commit

Permalink
fixed; populating empty arrays works
Browse files Browse the repository at this point in the history
closes #481
  • Loading branch information
aheckmann committed Aug 28, 2011
1 parent ddd3d32 commit 7b58bdb
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/model.js
Expand Up @@ -189,8 +189,8 @@ Model.prototype.init = function (doc, query, fn) {
} else {
if (obj[i] && schema && populate[path]) {
if (populate[path].sub) {
if (0 === obj[i].length) return next();
var total = 0;

obj[i].forEach(function (subobj) {
for (var key in populate[path].sub) {
if (subobj[key]) {
Expand Down
6 changes: 4 additions & 2 deletions lib/query.js
Expand Up @@ -847,8 +847,10 @@ Query.prototype.findOne = function (callback) {
if (!doc) return promise.complete(null);

var casted = new model();
delete casted._doc._id; // Remove the _id, so that pre inits do not have
// an _id

// Remove the _id, so that pre inits do not have one
delete casted._doc._id;

casted.init(doc, self, function (err) {
if (err) return promise.error(err);
promise.complete(casted);
Expand Down
29 changes: 28 additions & 1 deletion test/model.ref.test.js
Expand Up @@ -587,6 +587,33 @@ module.exports = {
});
});
});
}
},

// gh-481
'test populating subdocuments partially with empty array': function () {
var db = start()
, BlogPost = db.model('RefBlogPost', posts)
, worked = false;

var post = BlogPost.create({
title: 'Woot'
, comments: [] // EMPTY ARRAY
}, function (err, post) {
should.strictEqual(err, null);

BlogPost
.findById(post._id)
.populate('comments._creator', ['email'])
.run(function (err, returned) {
db.close();
worked = true;
should.strictEqual(err, null);
returned.id.should.equal(post.id);
});
});

setTimeout(function () {
worked.should.be.true;
}, 1700);
}
};

0 comments on commit 7b58bdb

Please sign in to comment.