简体   繁体   中英

Mongoose statics not found

Created a statics in a userModel.js such as userSchema.statics.login, and trying to refer to it in other file, but WebStorm is saying that is Unresolved function or method login(), pls help).

userSchema.statics.login = async function (email, password)  {
const user = await this.findOne({ email });
if(user){
    const auth = await bcrypt.compare(password, user.password);
    if(auth){
        return user;
    }
    throw Error('Incorrect password');
}
throw Error('Email doesn`t exist');
}
const User = mongoose.model('user', userSchema);
module.exports = User;

File in which i trying to refer:

const User = require("../models/userModel");
module.exports.login_post = async function (req, res) {
  const { email, password } = req.body;

  try {
      const UserLogin = await User.login(email,password);
    res.status(200).json({user: UserLogin._id});
  } catch (err) {
    res.status(400).json({})
  }

}

Fixed that by created a variable at the top of the file in which i want to call that function. Like that:

let User;
User = require("../models/userModel");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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