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.
Please sign in to leave a comment.
please can you provide the whole User module definition?
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)
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
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.
Anyway thanks for your help and swift response.
Can you think of any workaround in the meantime ?
Thanks
You can try using JSDoc here... for example:
Thanks a lot, this is of great help!!!
Do you know if you could implement seemless integration of Mongoose package inside Jetbrains suite
Please feel free to file a request for this feature to youtrack, https://youtrack.jetbrains.com/issues/WEB
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.
@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! :)
this issue is tracked as https://youtrack.jetbrains.com/issue/WEB-26542, please follow it for updates