Jakub Moz
Jakub Moz

Reputation: 137

VSCode module.exports autocomplete doesn't work

Using latest VSCode version 1.30.2 and it doesn't see the functions that are inside the exported module.

this is in model.js

var userModel = mongoose.model('userModel', usersSchema);
userModel.isUsernameTaken = isUsernameTaken;
module.exports = userModel;

function isUsernameTaken(username) {
    return userModel.findOne({username:username});
}

and in app.js

var userModel = require('./model');

Now upon typing userModel. in app.js I should see a suggestion for autocompletion of isUsernameTaken, but it's not there and also not any of the functions declared in model are "visible". However if I type the exact function name (case-sensitive). (ex: userModel.isUserNameTaken(etc)) it works. What is wrong ?

Upvotes: 1

Views: 2989

Answers (3)

Kartoos
Kartoos

Reputation: 1160

For me, (although not in the context of mongoose), I was having the same issue. autocomplete was not working and module.exports was also not turning into green color in VSCode. The fix for me was that I accidentally put an ES6 import in the file:

import config from 'config' // wrong way of importing
// my code
module.exports = {
  myObj
}

I simply changed it to use require

const config = require('config')

and it started working properly

Upvotes: 0

Jakub Moz
Jakub Moz

Reputation: 137

I managed to fix it by changing in model.js

module.exports.default = userModel;

and then in another file:

var userModel = require(./model).default;

Now intellisense works as it should.

Upvotes: 1

ca1c
ca1c

Reputation: 1285

When you say userModel.isUsernameTaken(username), what you are really saying is mongoose.model('userModel', usersSchema).isUsernameTaken(username), in which this would return as undefined. What you would have to do is make user Model into an object with the mongoose.model('userModel', usersSchema) inside of it. Kind of like this:

var userModel = function () {
    this.model: mongoose.model('userModel', usersSchema),
    this.isUsernameTaken: (username) => {
        return this.model.findOne({username:username});
    }
};

Then if you wanted to access these attributes you could use var user = new userModel(); then use user.isUsernameTaken(/*put username here*/);. Or if you wanted to access the model alone you could do: user.model. I hope this answers your question.

Upvotes: 0

Related Questions