LearnBoost/mongoose — Issue #568: populate doesn't work with ids of type String
Author: lackac
Created: 2011-10-18 16:31:35+00:00
State: open

mongoose has a problem populating properties where the dbref is not of type `ObjectId`. The issue came up when I tried to use plain `String` as the type of one of our models' `_id` property. I haven't tried, but I think using other types would have the same issue.

I'm guessing that this is caused by type conversion, since the value of the populated property is `'[object Object]'`.

Here's a test case that reproduces the issue:

``` javascript
var async = require('async')
, assert = require('assert')
, mongoose = require('mongoose')
, Schema = mongoose.Schema;

mongoose.connect('mongodb://localhost/test');

var UserSchema = new Schema({
_id: String
, name: String
})
, NoteSchema = new Schema({
author: { type: String, ref: 'User' }
, body: String
})
, User = mongoose.model('User', UserSchema)
, Note = mongoose.model('Note', NoteSchema)
, alice = new User({_id: 'alice', name: "Alice"})
, note = new Note({author: 'alice', body: "Buy Milk"});

async.parallel([

// save docs
function (done) { User.remove(function() { alice.save(done); }); }
, function (done) { Note.remove(function() { note.save(done); }); }

], function () {

// testing note.author.name
Note.findById(note.id).populate('author').run(function (err, note) {

// sanity check
assert.equal(note.body, "Buy Milk");

// here's the issue
assert.equal(typeof note.author, "object");
assert.equal(note.author.name, "Alice");

mongoose.connection.close();

});

}
```