Search code examples
javascriptmongodbvisual-studio-codemongooseintellisense

VS code Intellisense not working for mongodb models methods


I am writing a resolver function wherein I am expecting the VS intellisense to give suggestions for the mongodb methods e.g. when I type models.Note I am expecting 'find', 'findById' methods from VS intellisense.

const resolvers = {
  Query: {
    hello: () => 'Hello World',
    notes: async () => {
      return await models.Note.find();
    },
    note: async (_, args) => {
      return await models.Note.findById(args.id);
    }
  },

Solution

  • You can try to import the Note model directly. if IntelliSense still does not work, you can use jsdoc, actually you can use it in both situations.

    /** @type {import("mongoose").Model} */
    const Note = require("@models/note.model");
    
    // or
    
    /**
     * @typedef {object} DestructuredModel
     * @property {object} models
     * @property {import("mongoose").Model} models.Note
     */
    /** @type {DestructuredModel} */
    const { models } = require("mongoose");
    

    for further jsdoc

    and another answer about jsdoc may be useful