javascript Node.js 类型错误:未定义不是函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10419705/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 09:48:48  来源:igfitidea点击:

Node.js TypeError: undefined is not a function

javascriptnode.jsmongoose

提问by Feras Odeh

I'm trying to call this function on my model User(I'm using mongoose). like this:

我试图在我的模型用户上调用这个函数(我使用的是猫鼬)。像这样:

UserSchema.statics.exists = function exists(req,email, callback) {
    this.findOne({
        email : email
    }, function(err, user,callback) {
        if(err) {
            console.error(err);
            return callback(err);
        }
        if(!user) {
            // console.log("Not user");
            return callback(null, false);// produce error
        }
        if(!user.valid) {
            console.log("User invalid");
            var hostname = req.headers.host;
            // hostname = 'localhost:8080'
            //var pathname = url.parse(req.url).pathname; // pathname = '/MyApp'

            var base_url = 'http://' + hostname + '/activation?key=' + user.account_no;
            user.verifyEmail(base_url, user, function(err, result) {
                if(err) {
                    console.error(err);
                return  callback(err);
                } else {
                    //if(email sent)
                    if(result) {
                    return  callback("Please check your email to activate your account");
                    } else {
                    return  callback("Activation error please contact WOWITO support");
                    }
                }
            });
        }
        return callback(null, user);
    });
}

but then I got the following error:

但后来我收到以下错误:

node.js:201 throw e; // process.nextTick error, or 'error' event on first tick ^ TypeError: undefined is not a function

node.js:201 抛出 e; // process.nextTick 错误,或第一个滴答时的 'error' 事件 ^ TypeError: undefined is not a function

What did I do wrong?

我做错了什么?

Thanks,

谢谢,

回答by Jonathan Lonowski

You have 2 different callbackvariables, currently:

callback目前有 2 个不同的变量:

UserSchema.statics.exists = function exists(req, email, callback) { // 1st
    this.findOne({
        email : email
    }, function(err, user, callback) { // 2nd
    // ...

As they share the same identifier, the 2nd will "shadow" the 1st, rendering the 1st inaccessible within the anonymous function.

由于它们共享相同的标识符,因此第二个将“隐藏”第一个,从而使匿名函数中的第一个无法访问。

To use the 1st, you'll have to rename one of them -- perhaps, existsCallbackand/or findOneCallback.

要使用第一个,您必须重命名其中一个 - 也许,existsCallback和/或findOneCallback.

You may also be able to outright remove the 2nd, since it seems to be undefinedanyways:

您也可以完全删除第二个,因为它似乎undefined无论如何:

UserSchema.statics.exists = function exists(req, email, callback) {
    this.findOne({
        email : email
    }, function(err, user) {
    // ...

You're also assuming that a value is being passed for callback, which JavaScript doesn't actually require or enforce.

您还假设正在为 传递一个值callback,而 JavaScript 实际上并不要求或强制执行该值。

You can resolve this by testing for a value before calling:

您可以通过在调用之前测试一个值来解决这个问题:

if (callback) callback(...);

Or set it to a "no-op" function when it's not defined:

或者在未定义时将其设置为“无操作”功能:

callback = callback || function() { return true; };
//...
callback(...);