I recently started rewriting my Rails side project, what.pm, to Express. One reason is that I want to get better at JavaScript, the other is that R...
For further actions, you may consider blocking this person and/or reporting abuse
Update below:
Thanks for starting this post. I am using Mongoose 5.3 and I'm having issues with making changes to my existing schema.
I don't have each model in different files, they are all in the same file and then get exported as part of a single app schema, so how would I access Base within the same file. I tried something like this:
However, when I try to load my app, it complains with error:
Base.discriminator is not a function
I tried upgrading mongoose to version
5.4.2
and I am still getting this error.UPDATE: My issue seems related to the fact that I need to work on a deeply embedded doc and this array of objects is what needs to be able to inherit from the base class. Example,
person
object with embeddedbelongings
array, which contains amedia
array, where the movies, books, etc. are store and these media items need to share a schemaSame with me, did u solve it?
Hi, Thank you so much for your post. I have a question. I did my models like above. However, I don't have different colletions for my models in database. Is it normal? Additionally, I want to different collection for my models. How can I do it? Could you share any source or give information?
Hello , did you solve it please?
Thanks a lot for using the perfect example to demonstrate the use cases and output. Many other articles and even the original documentation used some complex event logging example which a beginner like me couldn't make any sense of.
Nienke, great article that helped me a lot! What would be the routes from app.js to this Schemes.. ? Routes via models/book.js, models/tvshow.js or via the models.base.js? Many thanks, Peter Tichelaar
Hi Peter,
I set up my routes in
app.js
like this:And then in routes.js, I call my controllers:
and inside baseController, I call my models like this:
If I want to, for example, get an item by its ID, I do this inside my controller:
Then in my routes, I do this:
Hope that helps any? I'm planning on open sourcing my code at some point, it just needs a lot of cleaning up :/
Thank you so much for the post. This was the only post on the internet that described mongoose discriminator function clearly.
thanks for your post. I have a question. I wonder, how i could select all of documents in my collection will all discriminators included ?
Nice article!, been looking for throughout explanation about this
Very clear step by step article! Found this super useful!
Thanks for sharing this. It was easy to understand. I applied it in my "user role access" system and it worked.
This is a really helpful post which quickly and greatly show how to use mongoose discriminators! Thank you!
...but, how do you actually create a new derived schema???
Thanks man! I really liked your blog.
GREAT Post indeed ..........
but Plz explain me what is purpose of BaseOptions ???
Brilliant article Nienke, great choice of use-case to illustrate an OO approach to Express+ Mongoose