1

我正在使用mysql我的 nodejs 项目。

这是我的sql代码:

var mysql = require('mysql');

var config = {
    host: 'localhost',
    username: 'root',
    password: 'root',
    port: 8889,
    database: "BookDB",
    connectionLimit: 100,
    charset: 'UTF8_GENERAL_CI',
    dialect: "mysql"
};


var pool = mysql.createPool({
   connectionLimit: config.connectionLimit,
    host     : config.host,
    user     : config.username,
    password : config.password,
    database : config.database,
    port     : config.port,
    charset  : config.charset,
    dialect  : config.dialect
});

module.exports.pool = pool;

这是我的数据库查询代码。我分别插入数据库 3 个表:用户、配置文件、活动。我的问题是:我总是只运行两个查询,最后一个查询看起来从未接触过。我打印错误但什么也没显示。

例如:如果我插入(User, Profile, Activity). 只是Activity表不能插入。如果我插入(User, Activity, Profile). 只是Profile表不能插入。这意味着我对每个表的插入代码都是正确的。

var async = require('async');
var pool = require('../config/mysql').pool;

var createUser = function createUser (username, password, email, firstName, lastName, avatarURL, // user table
                                      quote, about, homepage,                                    // profile table
                                      lastLoginIP,                                               // activity table
                                      callback) {
    // database connection
    var dbc;
    // user id for later reference
    var userId;
    var dateCreated = new Date();
    var dateUpdated = dateCreated;

    async.waterfall([
       // get connection
        function (callback) {
            pool.getConnection(callback);
        },

        // insert user table
        function (connection, callback) {
            dbc = connection;
            var params = {
                Username: username,
                Password: password,
                EmailAddress: email,
                FirstName: firstName,
                LastName: lastName,
                avatarURL: avatarURL,
                DateCreated: dateCreated,
                DateUpdated: dateUpdated
            };
            var query = "INSERT INTO USER SET ?";
            dbc.query(query, params, callback);
        },

        // insert activity table
        function(result, callback) {
            userId = result.insertId;
            console.log('second user id: ' + userId);
            var params = {
                ActivityID: userId,
                ProfileView: 0,
                LastLoginIP: lastLoginIP
            };
            var query = "INSERT INTO ACTIVITY SET ?";
            dbc.query(query, params, callback);
        },

        // insert profile table
        function (result, callback) {
           // userId = result.insertId;
            console.log('inserted userid: '+ userId);
            var params = {
                ProfileID: userId,
                Quote: quote,
                About: about,
                HomePage: homepage,
                DateCreated: dateCreated,
                DateUpdated: dateUpdated
            };
            var query = "INSERT INTO PROFILE SET ?";
            dbc.query(query, params, callback);
        },



        function (error, userData) {
            console.log('end block');
            if (dbc) dbc.release();
            if (error) {
                console.log('error');
                report_error(error);
            } else {
                callback(null, userData);
            }
        }
    ]);

};

module.exports.createUser = createUser;

请弄清楚我的问题。

谢谢 :)

4

1 回答 1

1

你应该把你的callback函数放在waterfall数组之外。像这样:

var async = require('async');
var pool = require('../config/mysql').pool;

var createUser = function createUser (username, password, email, firstName, lastName, avatarURL, // user table
                                  quote, about, homepage,                                        // profile table
                                  lastLoginIP,                                               // activity table
                                  callback) {
// database connection
var dbc;
// user id for later reference
var userId;
var dateCreated = new Date();
var dateUpdated = dateCreated;

async.waterfall([
   // get connection
    function (callback) {
        pool.getConnection(callback);
    },

    // insert user table
    function (connection, callback) {
        dbc = connection;
        var params = {
            Username: username,
            Password: password,
            EmailAddress: email,
            FirstName: firstName,
            LastName: lastName,
            avatarURL: avatarURL,
            DateCreated: dateCreated,
            DateUpdated: dateUpdated
        };
        var query = "INSERT INTO USER SET ?";
        dbc.query(query, params, callback);
    },

    // insert activity table
    function(result, callback) {
        userId = result.insertId;
        console.log('second user id: ' + userId);
        var params = {
            ActivityID: userId,
            ProfileView: 0,
            LastLoginIP: lastLoginIP
        };
        var query = "INSERT INTO ACTIVITY SET ?";
        dbc.query(query, params, callback);
    },

    // insert profile table
    function (result, callback) {
       // userId = result.insertId;
        console.log('inserted userid: '+ userId);
        var params = {
            ProfileID: userId,
            Quote: quote,
            About: about,
            HomePage: homepage,
            DateCreated: dateCreated,
            DateUpdated: dateUpdated
        };
        var query = "INSERT INTO PROFILE SET ?";
        dbc.query(query, params, callback);
    }
],
    function (error, userData) {
        console.log('end block');
        if (dbc) dbc.release();
        if (error) {
            console.log('error');
            report_error(error);
        } else {
            callback(null, userData);
        }
    }
)};

module.exports.createUser = createUser;
于 2015-07-20T21:00:59.377 回答