简体   繁体   English

TypeError:SomeFunction不是函数(使用Mongoose)

[英]TypeError: SomeFunction is not a function (using Mongoose)

I have this error: TypeError: User.getUserByUsername is not a function at Strategy._verify (.../routes/users.js:65:10) 我遇到此错误:TypeError:User.getUserByUsername不是Strategy._verify的函数(... / routes / users.js:65:10)

var User = require('../models/user');

passport.use(new LocalStrategy(
  function(username, password, done) {
    User.getUserByUsername(username, function(err,user){
    if(err) throw err;
    if (!user){
        return done(null, false, {message: 'Unknow User'});
    }

    User.comparePassword(password, user.password, function(err, isMatch){
        if(err) throw err;
        if(isMatch){
            return done(null,user);
        } else {
                return done(null, false, {message: "Invalid password"});
        }
    });
   });
  }));

The function is defined in models/user.js 该函数在models / user.js中定义

module.exports.getUserByUsername = function(username, callback){
var query = {username: username};
User.findOne(query, callback);
 }

Check what the ../models/user module is exporting. 检查../models/user模块要导出什么。 From your description of the problem, it exporting something, but that something doesn't have a getUserByUsername function -- is that module perhaps exporting a prototype or class, which you should instantiate, eg: 从对问题的描述中,它可以导出某些内容,但是该内容不具有getUserByUsername函数-该模块可能导出的是原型或类,您应该实例化,例如:

var UserModel = require('../models/user');
var user = new UserModel();
...

From the looks of it, you're using Mongoose (always good to mention in your questions). 从外观上看,您正在使用猫鼬(总是在您的问题中提到)。 If not, disregard the following (but please update your question to signify which ORM you're using). 如果不是,请忽略以下内容(但请更新您的问题以表示您正在使用哪个ORM)。

I think that you might be doing this in models/user.js : 我认为您可能在models/user.js这样做:

module.exports.getUserByUsername = function(...) {}
...
module.exports = mongoose.model('User', ...);

The latter assignment will replace module.exports , therefore making getUserByUsername unreachable. 后面的分配将替换module.exports ,因此使getUserByUsername无法访问。

Instead, it's probably better to make getUserByUsername a static method , just like comparePassword : 相反,最好将getUserByUsername 静态方法 ,就像comparePassword一样:

userSchema.statics.getUserByUsername = function(...) {}

Try below , Change your User.js file as below: 请尝试以下操作,如下更改您的User.js文件:

exports.getUserByUsername = function(username, callback){
  var query = {username: username};
  User.findOne(query, callback);
};

Could it be that you assign to the module.exports later down the file? 可能是您分配给module.exports稍后在文件下module.exports吗? Exporting members should be declared after assigning to module.exports or exports 分配给module.exportsexports后,应声明导出成员

Eg 例如

    module.exports.getUserByUsername = function() {...};

    module.exports = User;

would be a common issue that resembles yours. 会是与您类似的常见问题。

The issue was because of a wrong declaration of one of the function of the process flow. 问题是由于错误地声明了流程流的功能之一。 All the functions have to be declared the same way in order to export correctly all the object. 为了正确导出所有对象,必须以相同的方式声明所有函数。

module.exports.getUserByUsername = function()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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