简体   繁体   中英

nodejs prototype and nested callbacks with mysql

I have two problems with the code below. It is the beginning of a node.js module for registering users that are stored in a mysql database.

The first problem is that in the callback from the first query the variable this.connection is undefined. The second problem is that the variable user is also undefined in the same callback. Hence the code crashes on the following line:

this.connection.query('INSERT INTO users{{values values}}', {
                    values: {
                        user_name: user.username,
                        user_email: user.email,
                        user_password: user.password
                    }
                })

Please let me know how to solve this in a propper way.

Many thanks!

Full code is below:

module.exports = usermodel;

//connection is a mysql-wrapper connection
function usermodel(connection)
{
    this.connection = connection;
}

playermodel.prototype = {

    registerByEmail: function (user, callback) {
        //check firts if the user already exists
        this.connection.query('SELECT user_id FROM users {{where data}}', {
            data: {
                user_email: user.email
            }
        },
        function (err, result) {

            if (err) {
                ...
            } else if (result.length > 0) {
                ...
            } else {
                // this.connection is undefined ????????????
                this.connection.query('INSERT INTO users {{values values}}', {
                    values: {
                        user_name: user.username,
                        user_email: user.email,
                        user_password: user.password
                    }
                }),
                function (err, result) {
                    ...
                }
            }
        });
    }

}

this.connection is undefined because this.connection is on the instance of playermodel the callback function passed to the first query does not know about the context of the playermodel instance.

user should be defined though. You can get around the this.connection being undefined by either binding the function or setting a reference to this in a variable the the callback will have access to.

//Bind
        playermodel.prototype = {

        registerByEmail: function (user, callback) {
            //check firts if the user already exists
            this.connection.query('SELECT user_id FROM users {{where data}}', {
                data: {
                    user_email: user.email
                }
            },
            function (err, result) {

                if (err) {
                    ...
                } else if (result.length > 0) {
                    ...
                } else {
                    // this.connection is undefined ????????????
                    this.connection.query('INSERT INTO users {{values values}}', {
                        values: {
                            user_name: user.username,
                            user_email: user.email,
                            user_password: user.password
                        }
                    }),
                    function (err, result) {
                        ...
                    }
                }
            //HERE
            }.bind(this));
        }

    }

or

    //me reference to this
        playermodel.prototype = {

            registerByEmail: function (user, callback) {
                //HERE
                var me = this;
                //check firts if the user already exists
                this.connection.query('SELECT user_id FROM users {{where data}}', {
                    data: {
                        user_email: user.email
                    }
                },
                function (err, result) {

                    if (err) {
                        ...
                    } else if (result.length > 0) {
                        ...
                    } else {
                        //HERE
                        me.connection.query('INSERT INTO users {{values values}}', {
                            values: {
                                user_name: user.username,
                                user_email: user.email,
                                user_password: user.password
                            }
                        }),
                        function (err, result) {
                            ...
                        }
                    }
                });
            }

        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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