Node.js TypeError: undefined is not a function

综合技术 2018-03-07

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,

Problem courtesy of: Feras Odeh

Solution

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(...);

Solution courtesy of: Jonathan Lonowski

您可能感兴趣的

Cloud9: nodejs server image not getting rendered (... I have following setup in cloud9 IDE. Project root folder Hello.html - contains simple html tags (+image tag) Preview displays ...
打造跳跃音波播音乐放器(Electron+Nodejs+React)... Electron可以让我们使用html,css,javascript来搭建跨平台(Windows、macOS、Linux)的桌面应用。下面通过Electron+Nodejs+React来实现一个支持播放在线音乐及本地音乐的播放器。播放器设计风格为windows的Fluent Design,wi...
Sequelize 5.0.0 和 5.0.1 发布,Node.js 的 ORM Sequelize 5.0.0 和 5.0.1 发布了。Sequelize.js 提供对MySQL, MariaDB ,SQLite和 PostgreSQL数据库的简单访问,通过映射数据库条目到对象,或者对象到数据库条目。简而言之,就是 ORM(Object-Relational-Mapper)。...
Node-Web-Console: A simple web-based SSH, remote s... English | 简体中文 _ __ __ _ __ __ ______ __ / | / /___ ____/ /__ | | /...
Node.js with ZeroMQ + Socket.io + scoping per clie... I'm writing a web application using node.js & express to provide a real-time aprs stream to each user via ZeroMQ & socket.io. ...