buunguyen / mongoose-deep-populate

Mongoose plugin to enable deep population of nested models ⛺
MIT License
469 stars 44 forks source link

Nested object #47

Open kevinresol opened 8 years ago

kevinresol commented 8 years ago

I have a schema like this:

new Schema({
    waitingList: {
        __type__: {
            current: {
                __type__: [{
                    date: {
                        __type__: Date
                    },
                    user: {
                        __type__: ObjectId,
                        ref: "User"
                    },
                }]
            },
            history: {
                __type__: [{
                    date: {
                        __type__: Date
                    },
                    user: {
                        __type__: ObjectId,
                        ref: "User"
                    },
                }]
            }
        }
    },
}, {
    autoIndex: true,
    typeKey: "__type__"
});

So, in essence a sample document looks like this:

{
  waitingList: {
    current: [],
    history: [{date: someDate, user: myUser}],
  }
}

In that case I want to deep populate user, but if I specify the path as "waitingList.history.user". The plugin crashes with error TypeError: Cannot read property 'paths' of undefined at this position: https://github.com/buunguyen/mongoose-deep-populate/blob/0080a0949fef38bcb9541b267a55810c14c78d4c/lib/plugin.js#L260

For some funny reason I am restricted from declaring sub-schema for those embeded documents. It is possible to make it work in such case?

kevinresol commented 8 years ago

This is what I get if I log schemaPath at line 261

Mixed {
  path: 'waitingList',
  instance: 'Mixed',
  validators: [],
  setters: [],
  getters: [],
  options: { __type__: { current: [Object], history: [Object] } },
  _index: null }

So, at line 285 the assignment schema = schemaPath.schema will cause schema become undefined, causing the aforementioned crash.

buunguyen commented 8 years ago

What is __type__?

kevinresol commented 8 years ago

It is the typeKey http://mongoosejs.com/docs/guide.html#typeKey

buunguyen commented 8 years ago

If you can add a failed test to the bugs section, it would help.

kevinresol commented 8 years ago

Here you are: https://github.com/kevinresol/mongoose-deep-populate/commit/49bd67c0a27e227ecc3d71fb79052bfeb7db269b

buunguyen commented 8 years ago

Where can I find out more about this usage of type?

For example, what is the difference between

var ItemSchema = new Schema({
  waitingList: {
    type: {
      current: {
        type: [{
          date: {type: Date},
          user: {type: Number, ref: "User.bug47"},
        }]
      }
    }
  }
})

and

var ItemSchema = new Schema({
  waitingList: {
    current: [{
      date: {type: Date},
      user: {type: Number, ref: "User.bug47"},
    }]
  }
})
kevinresol commented 8 years ago

I am not sure if there are any extra information out there other than the link I posted and the source code. I think mongoose will force treating the the field as a type declaration if the field name matches the typeKey option (which defaults to "type")

AFAIK, there should be no different between the two examples, functionally.

Yuliang-Lee commented 8 years ago

Is this has solution?My code has same problems if key was named "type".

kevinresol commented 8 years ago

@Yuliang-Lee your problem is probably unrelated to deep-populate You may need to specify typeKey in the schema options.

Yuliang-Lee commented 8 years ago

@kevinresol I try to modify typeKey but deep-populate occurs error