简体   繁体   English

Node.js TypeError:未定义不是函数

[英]Node.js TypeError: undefined is not a function

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; node.js:201抛出e; // process.nextTick error, or 'error' event on first tick ^ TypeError: undefined is not a function // process.nextTick错误,或第一次滴答时发生“错误”事件^ TypeError:未定义不是函数

What did I do wrong? 我做错了什么?

Thanks, 谢谢,

You have 2 different callback variables, currently: 您有2个不同的callback变量,当前:

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, existsCallback and/or findOneCallback . 要使用1st,您必须重命名其中之一-也许是existsCallback和/或findOneCallback

You may also be able to outright remove the 2nd, since it seems to be undefined anyways: 您也许还可以彻底删除第二个,因为无论如何它似乎都是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(...);

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

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