简体   繁体   中英

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 ?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM