Mongoose: Mongoose static methods autocompletion in WebStorm

Created on 5 Jul 2016  路  8Comments  路  Source: Automattic/mongoose

When doing the following in WebStorm, I cannot get autocompletion when importing a mongoose model and trying to use a static method on the schema.

In one file I have my Mongoose schema defined with static methods:

 userSchema.statics.saveUser = function(callback){
        var user = new this();
        user.save(callback);
    };

And in another file I try to use these methods and I don't get autocompletion...

var User = require('../models/user.js');
User.saveUser(function(err, result) {
...
}

Whereas if I would type User.statics.saveUser(... I would get autocompletion.

Do you know about a workaround ? Thanks.

help wanted

Most helpful comment

I opened a feature request in JetBrains.
https://youtrack.jetbrains.com/issue/WEB-22317
Please upvote it so that they can take us seriously.
Would be great to have first class support for Mongoose inside Jetbrains IDEs.

Thanks !

All 8 comments

I opened a feature request in JetBrains.
https://youtrack.jetbrains.com/issue/WEB-22317
Please upvote it so that they can take us seriously.
Would be great to have first class support for Mongoose inside Jetbrains IDEs.

Thanks !

Would love to help but I've never used webstorm before so I'm afraid this will be a pretty time consuming task

Linking: #3870 & #3707

@vkarpov15 btw, what IDE do you use mostly?

I'm deathly allergic to IDEs, I just use atom and vim

@cyberjoac @Jeff-Lewis I do something similar in my Mongoose using projects in WebStorm (currently 2017.2 EAP build WS-172-3198.20) with no problem.

If I want to reference a lower-level function directly, I do it by defining that reference in the export statement of the field where the schema is defined:
module.exports = { saveUser: userSchema.statics.saveUser };

Then I can import that in the file where used directly:
var User = require('../models/test-user.js').saveUser;

With that, I get full support for autocompletion.
screen shot 2017-07-03 at 10 26 54 am

Personally, I don't export the schema or functions, I declare the model in the same file with the schema and just export the model as that is generally what is being used. But of course, your use-case or practices may be different.

Any update on this feature?

Nope. If anyone has expertise in webstorm they're quite welcome to write a plugin and I'd be happy to help, but I have never even installed webstorm so I wouldnt really know where to start.

Was this page helpful?
0 / 5 - 0 ratings