Mongoose static methods autocompletion in WebStorm

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.

12 comments
Comment actions Permalink

please can you provide the whole User module definition?

0
Comment actions Permalink
// grab the things we need
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var async = require('async');
var config = require('../config/config');

// create a schema
var IdConnectionSchema = new Schema({
name: String, // SQL(Live)
});

// on every save, add the date
IdConnectionSchema.pre('save', function(next) {
// get the current date
var currentDate = new Date();

// change the updated_at field to current date
this.updated_at = currentDate;

// if created_at doesn't exist, add to that field
if (!this.created_at)
this.created_at = currentDate;

next();
});

// Model level operations.
// Could define this method in the controller, but not the right place.
IdConnectionSchema.statics.doSomething = function(doSomethingCallback) {
// do whatever
doSomethingCallback(null);
};

var IDConnection = mongoose.model('ID_Connection', IdConnectionSchema);

// make this available to our users in our Node applications
module.exports = IDConnection;
0
Comment actions Permalink

I would like the doSomething method be in autocomplete whenever I do 

var IdConnection = require('./idConnectionModel');

idConnection.doSomething.

c.f: http://mongoosejs.com/docs/guide.html (statics section)

0
Comment actions Permalink

I can see 2 problems here:

- WebStorm knows nothing about Mongoose 'methods' and 'statics' objects, it has no way to understand that these objects are used to create custom instance and static methods for Models. Understanding these constructions requires providing special support for Mongoose

- completion doesn't work when re-exporting module instance (https://youtrack.jetbrains.com/issue/WEB-17099), so neither predefined nor custom methods are available in completion if Model is defined as a separate module

0
Comment actions Permalink

Any chance you could implement special support for Mongoose ? 
It is a VERY popular module for Node.JS and I'm sure it could benefit lots of developers and this way you could acquire lots of new customers which feel NodeJS support as of today is not good enough...

Thanks for your help.

0
Comment actions Permalink

Anyway thanks for your help and swift response.

Can you think of any workaround in the meantime ?
Thanks

0
Comment actions Permalink

You can try using JSDoc here... for example:

var userSchema = new Schema({
name: String,
password: String
});
/**
* @memberOf User#
*/
userSchema.methods.findSimilarNames = function(cb) {

};
/**
*
* @memberOf User
*/
userSchema.statics.findByName = function(name, cb) {

};

/** @class User */
var User = mongoose.model('User', userSchema);

module.exports = User;
0
Comment actions Permalink

Thanks a lot, this is of great help!!! 
Do you know if you could implement seemless integration of Mongoose package inside Jetbrains suite

0
Comment actions Permalink

Please feel free to file a request for this feature to youtrack, https://youtrack.jetbrains.com/issues/WEB

0
Comment actions Permalink

Go to Preferences . Languages&Frameworks . Javascript . Libraries . Download

scroll down to mongoose, select it, press "Download and install".

In case it isn't obvious, what you're doing is installing the typescript definitions for typescript code to understand mongoose.  Webstorm can use these to understand plain JavaScript, too.  

9
Comment actions Permalink

@Estaub2:

Done that!

__________________

This is still an issue and I'm trying to reproduce:

user-model.js

const mongoose = require('mongoose');

const UserSchema = mongoose.Schema({
name: {type: String}
});

const User = module.exports = mongoose.model('User', UserSchema);

module.exports.getByID = function (id) {

return User.findById(id)

};

some-controller.js

 

Whereas when i do this:

const User = module.exports.UserModel = mongoose.model('User', UserSchema);

I do not get 'direct' autocompletion, but at least Webstorm doesn't complain about 'unresolved variable':

In VS Code i.e it does work. Can we get a fix please? I DO LOVE JETBRAINS! :)

 

 

0
Comment actions Permalink

this issue is tracked as https://youtrack.jetbrains.com/issue/WEB-26542, please follow it for updates

0

Please sign in to leave a comment.