Sunday, 4 June 2017

mongoose nested types population (nested type declaration)

I have a problem with mongoose v4.10.4.

I have a Page defined like so:

Page = {
  title: String,
  creator: { type: Schema.Types.ObjectId, ref: 'User', required: true }
}

Now i want to populate the creator with .populate('page.creator')

If define page like this

page: Page // .populate('page.creator') does work

it works just fine, but if i define it like this to set a default value

page: { type: Page, required: true, default: {} } // .populate('page.creator') does not work

it does not work and returns page.creator = null.

What am i doing wrong here?

EDIT:

This also does NOT work.

page: { type: {
      title: String,
      creator: { type: Schema.Types.ObjectId, ref: 'User', required: true }
    }, required: true, default: {} }



via White Noise

No comments:

Post a Comment