mongoose-deep-populate icon indicating copy to clipboard operation
mongoose-deep-populate copied to clipboard

Nested object

Open kevinresol opened this issue 9 years ago • 10 comments

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 avatar Jun 14 '16 07:06 kevinresol

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.

kevinresol avatar Jun 14 '16 08:06 kevinresol

What is __type__?

buunguyen avatar Jun 14 '16 17:06 buunguyen

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

kevinresol avatar Jun 14 '16 19:06 kevinresol

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

buunguyen avatar Jun 14 '16 20:06 buunguyen

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

kevinresol avatar Jun 15 '16 02:06 kevinresol

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"},
    }]
  }
})

buunguyen avatar Jun 15 '16 17:06 buunguyen

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.

kevinresol avatar Jun 15 '16 23:06 kevinresol

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

Yuliang-Lee avatar Jun 22 '16 10:06 Yuliang-Lee

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

kevinresol avatar Jun 22 '16 10:06 kevinresol

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

Yuliang-Lee avatar Jun 23 '16 07:06 Yuliang-Lee