How to get a list of available Mongoose Discriminators?

限于喜欢 提交于 2019-12-05 02:29:10

Since v4.11.13, mongoose model has model.discriminators which is an array of models, keyed on the name of the discriminator model.

In your case if you do console.log(User.discriminators) you will get:

{
  Client: {
    ....
  },
  Employee: {
  }
}

As far as I can see, this is not documented anywhere.

Line 158 in lib.helpers.model.discriminators.js is where this is created.

I think you want to fetch the names and values of all the discriminators as for the names you can simply use

User.discriminators

but for finding values you can use this

return Promise.all(Object.keys(discriminators).map(i => 
    discriminators[i].find({ userId: this._id }))
).then(promiseResults =>
    promiseResults.reduce((arr, el) => arr.concat(el), [])
);

you need to put userId under each discriminators for that.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!