Skip to content

Commit

Permalink
fixed; saving populated new docs
Browse files Browse the repository at this point in the history
closes #1442
  • Loading branch information
aheckmann committed Apr 19, 2013
1 parent 57e8bf6 commit a48c405
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
3 changes: 2 additions & 1 deletion lib/document.js
Expand Up @@ -1427,7 +1427,7 @@ Document.prototype.$__doQueue = function () {
*/

Document.prototype.toObject = function (options) {
if (options && options.convertToId) {
if (options && options.depopulate && !options.isParent) {
// populated paths that we set to a document?
Embedded || (Embedded = require('./types/embedded'));
if (!(this instanceof Embedded)) {
Expand All @@ -1444,6 +1444,7 @@ Document.prototype.toObject = function (options) {
}

;('minimize' in options) || (options.minimize = this.schema.options.minimize);
options.isParent = false;

var ret = clone(this._doc, options);

Expand Down
4 changes: 2 additions & 2 deletions lib/model.js
Expand Up @@ -170,7 +170,7 @@ Model.prototype.save = function save (fn) {

if (this.isNew) {
// send entire doc
var obj = this.toObject({ depopulate: 1 });
var obj = this.toObject({ depopulate: 1, isParent: 1 });
this.$__version(true, obj);
this.collection.insert(obj, options, complete);
this.$__reset();
Expand Down Expand Up @@ -378,7 +378,7 @@ Model.prototype.$__delta = function () {
operand(this, where, delta, data, value);

} else {
value = utils.clone(value, { convertToId: 1 });
value = utils.clone(value, { depopulate: 1 });
operand(this, where, delta, data, value);
}
}
Expand Down
18 changes: 18 additions & 0 deletions test/document.populate.test.js
Expand Up @@ -485,4 +485,22 @@ describe('document.populate', function(){
})
})
})

describe('of new document', function(){
it('should save just the populated _id (gh-1442)', function(done){
var b = new B({ _creator: user1 });
b.populate('_creator', function (err, b) {
if (err) return done(err);
assert.equal('Phoenix', b._creator.name);
b.save(function (err) {
assert.ifError(err);
B.collection.findOne({ _id: b._id }, function (err, b) {
assert.ifError(err);
assert.equal(b._creator, String(user1._id));
done();
})
})
})
})
})
});

0 comments on commit a48c405

Please sign in to comment.