最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Node.js TypeError: undefined is not a function - Stack Overflow

programmeradmin1浏览0评论

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

What did I do wrong?

Thanks,

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

What did I do wrong?

Thanks,

Share Improve this question asked May 2, 2012 at 18:37 Feras OdehFeras Odeh 9,30620 gold badges79 silver badges122 bronze badges 1
  • normally one would see this if the callback was not actually a function. Seems like whoever's called this function has passed in bad parameters... – ControlAltDel Commented May 2, 2012 at 18:39
Add a ment  | 

1 Answer 1

Reset to default 4

You have 2 different callback variables, currently:

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.

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

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.

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(...);
发布评论

评论列表(0)

  1. 暂无评论