Reputation: 329
In my Node.js project (using VS Code IDE), Mongoose schema intellisense is not shown. I'm doing a Udemy Node.js course. In there, the course instructor's VS Code showed Mongoose schema intellisense. I mean when they typed "type" property, VS Code suggests the "type" property of Mongoose schema. Also for other properties like "required", "default", "min", "max", etc.
But, in my VS Code it doesn't show the mongoose schema intellisense.
I don't use TypeScript, it is just a Vanilla JS (Node.js) project.
I searched the solution for nearly all day long. But couldn't fix my problem.
Some say things like:
npm i mongoose -g
(doesn't work for me)npm i @types/mongoose
(this NPM package is deprecated 2 years ago)My Node.js version is v20.2.0.
My VS Code version is 1.78.2 "user" installer
My mongoose npm package version is [email protected]
I'm running on Windows 10
The Udemy instructor used
The Udemy Node.js course I'm taking is Maximilian Schwarzmüller's "NodeJS - The Complete Guide (MVC, REST APIs, GraphQL, Deno)" [https://www.udemy.com/course/nodejs-the-complete-guide/]
The screen shot of Udemy instructor's video is shown below. The link of that video is [https://www.udemy.com/course/nodejs-the-complete-guide/learn/lecture/11954140#content]. And timespan of the video screen shot is 03:46.
Here is screen shot for my problem (on my VS Code):
Here is my code:
/**
* models/Product.js (Product Model file)
*/
import mongoose from "mongoose";
const Schema = mongoose.Schema;
const productSchema = new Schema({
title: {
type: String,
required: true,
}
});
const Product = mongoose.model('Product', productSchema);
export default Product;
As one of the stackoverflow helper @starball asked, I provided the source of the instructor and my "npm list" below.
Here is Udemy Instructor's Source code [https://github.com/HponeThitHtoo/saving-mongoose] (@starball sorry for my typo in my previous. The Udemy instructor didn't upload his source code on GitHub. He just give a link of zip file in his Udemy Course". So, I uploaded his source code to my GitHub account.)
Here is my npm list @types/mongoose
Upvotes: 2
Views: 2673
Reputation: 51942
Your instructor used [email protected]
, which was at a point before Mongoose started publishing its own TypeScript type definitions in its own package, so you should npm i -D '@types/mongoose@~5.2.0'
. The ~
means anything matching 5.2
(any patch version under 5.2
). The reason why you should do that is because Definitely Typed packages are written to match the major and minor version of the JS package they provide typings for (source).
You shouldn't be surprised that what you're trying doesn't work when you're using major version 7 of Mongoose and your instructor is using major version 5. In semver, major versions mean breaking changes.
If you're on a newer major version of mongoose than major version 5, check if you even need the DefinitelyTyped package. For example, on npmjs.com, I can see that major version 8 already packages its own types instead of replying on a DefinitelyTyped package.
Upvotes: 0
Reputation: 1
The solution is simple but hard to find. Add this above your Schema
/**
* @type {mongoose.SchemaDefinitionProperty}
*/
Upvotes: 0