What is the difference between mongoose.Schema()
and new mongoose.Schema()
?
I used both the standards, and I think both do the same job. Does it affect in any way?
What is the difference between mongoose.Schema()
and new mongoose.Schema()
?
I used both the standards, and I think both do the same job. Does it affect in any way?
Share Improve this question asked Jan 23, 2020 at 1:52 Madhubala JayakumaranMadhubala Jayakumaran 391 silver badge7 bronze badges 3-
1
Are you sure that both do the same job? I dunno. Just asking too. It may be possible that they both do the same job. The
new
keyword there meansinstantiate a new object
though – orimdominic Commented Jan 23, 2020 at 2:02 - duplicate stackoverflow./questions/39719454/… – Shivam Pandey Commented Jan 23, 2020 at 7:49
- 1 Does this answer your question? Creating Mongoose Schemas with or without 'new' keyword? – Shivam Pandey Commented Jan 23, 2020 at 7:50
2 Answers
Reset to default 7Looking at the docs, it should always be called with the new
keyword, since Schema
is a constructor.
Looking at the source code, it'll return a newly allocated object if you try to use it without the new
keyword.
Both will work, but I would only use the first since it's the correct way. Following standard conventions will make your code easier to read; not only for others, but for you as well when you go back to it in 6 months.
Apart from the standard practice and documentation, the only difference that i have figured out is this : (VS code specific) VS code seems to recognise the model which are created using a schema(created using new ) . ie. MongoDB specific intellisense like query operations, CRUD methods seem to be working.
Note. Schemas created without new do work fine in all the cases but the CRUD related intellisense (at least for my case) do not work. Without new with new
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745321245a4622454.html
评论列表(0条)