Mongoose unresolved method or function with @types/mongoose installed

If I require a mongoose model like so: `const User = require('./user')`

WebStorm can't resolved those method like, `find()` or `save()` but the code obviously works. And `const {User} = require('./user')` all the method are resolved but the app breaks. Only when I do something like this, declaring a variable first, it works:

const express = require('express');
const router = express.Router();
const argon2 = require('argon2');
const jwt = require('jsonwebtoken');

let User;
User = require('./user');

router.get('/', async function(req, res, next) {
const users = await User.find({});
res.status(200).json(users);
});

router.post('/new', async (req, res, next) => {
const hash = await argon2.hash(req.body.password);
let user = new User({
email: req.body.email,
password: hash
});
await user.save();
res.status(201).json(user);
});

router.post('/login', async (req, res, next) => {
const email = req.body.email;
const user = await User.findOne({ email: email });

if (user) {
try {
if (await argon2.verify(user.password, req.body.password)) {
const token = jwt.sign(
{ id: user._id, email: user.email },
`${process.env.JWT}`,
{ expiresIn: '1h' }
);
res.status(200).json({ token: token });
} else {
res.status(401).json({ message: 'Incorrect credentials' });
}
} catch (e) {
console.log(e);
}
}
});

module.exports = router;


And I have @types for express and mongoose.
Why is that? What am I missing?

3 comments
Comment actions Permalink

please vote for https://youtrack.jetbrains.com/issue/WEB-40869 to be notified on any progress with it

0
Comment actions Permalink

I am not sure that if the same issue. I have auto completion. The method on a model just isn't resolved.

0

Please sign in to leave a comment.