简体   繁体   English

VSCode module.exports自动完成不起作用

[英]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. 使用最新的VSCode版本1.30.2,看不到导出模块内部的功能。

this is in model.js 这是在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 并在app.js中

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

Now upon typing userModel. 现在输入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". 在app.js中,我应该看到有关isUsernameTaken自动完成的建议,但它不存在,并且模型中声明的任何函数也不是“可见的”。 However if I type the exact function name (case-sensitive). 但是,如果我键入确切的函数名称(区分大小写)。 (ex: userModel.isUserNameTaken(etc)) it works. (例如:userModel.isUserNameTaken(etc)),它可以正常工作。 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. 当您说userModel.isUsernameTaken(username) ,您真正要说的是mongoose.model('userModel', usersSchema).isUsernameTaken(username) ,其中它将返回未定义状态。 What you would have to do is make user Model into an object with the mongoose.model('userModel', usersSchema) inside of it. 您需要做的是使用户模型成为其中包含mongoose.model('userModel', usersSchema)的对象。 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(); 然后,如果要访问这些属性,可以使用var user = new userModel(); then use user.isUsernameTaken(/*put username here*/); 然后使用user.isUsernameTaken(/*put username here*/); . Or if you wanted to access the model alone you could do: user.model . 或者,如果您想单独访问模型,则可以执行: user.model I hope this answers your question. 我希望这回答了你的问题。

I managed to fix it by changing in model.js 我设法通过更改model.js来修复它

module.exports.default = userModel;

and then in another file: 然后在另一个文件中:

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

Now intellisense works as it should. 现在,Intellisense可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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