简体   繁体   English

使用MySQL的Node.js原型和嵌套回调

[英]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. 它是用于注册存储在mysql数据库中的用户的node.js模块的开始。

The first problem is that in the callback from the first query the variable this.connection is undefined. 第一个问题是在第一个查询的回调中,变量this.connection是未定义的。 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. this.connection是未定义的,因为this.connection是在playermodel实例上的,传递给第一个查询的回调函数不知道playermodel实例的上下文。

user should be defined though. user应该被定义。 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. 通过绑定函数或在回调将有权访问的变量中设置this的引用,可以解决未定义的this.connection

//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) {
                            ...
                        }
                    }
                });
            }

        }

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

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