如果使用 node-mysql 在同一脚本中执行超过 10 次,则不会获得对同一查询的响应

Not getting response for same query if executed more than 10 times in same script using node-mysql

几个小时前我问过这个 question。当我关注这个当前问题时,我正在对其进行故障排除。

这是循环,如果我执行函数 selectPhotoById 超过 10 次,我只会得到 10 个查询的响应。

例如,对于以下循环。

for (var i = 0; i < 4; i++) {
    db.selectPhotoById("12246", function (res) {
        console.log(res[0].ID);
    });
}

在控制台响应中,我得到

12246
12246
12246
12246

这很好用,但如果我像

那样增加循环
for (var i = 0; i < 24; i++) {
    db.selectPhotoById("12246", function (res) {
        console.log(res[0].ID);
    });
}

我只收到 10 个 ID 作为响应

12246
12246
12246
12246
12246
12246
12246
12246
12246
12246

这是写入查询的文件的代码

var mysql = require('node-mysql');
var DB = mysql.DB;
var photo = new DB({
    host: 'localhost',
    user: 'root',
    password: 'root',
    database: 'photo'
});

function DataBase() { 
}

DataBase.prototype.selectPhotoById = function (photo_id, callback) {
    photo.connect(function (conn, cb) {
        conn.query("select * from photo where ID =" + photo_id, function (err, res) {
            if (err)
                throw err;

            return callback(res);
        });
    });
}

DataBase.prototype.insertThumb = function(photo_id, blob, size, callback){
    photo.connect(function(conn, cb){
       var query = 'INSERT INTO photo.photo_thumb (`photo_id`, `thumb`, `size`) values ("'+photo_id+'", "'+blob+'", "'+size+'")';
       conn.query(query, function (err, res){
            if (err) throw err;
            return callback(res);
       }); 
    });
}

DataBase.prototype.checkThumb = function(photo_id, size, callback){
    photo.connect(function(conn, cb){
        var query = 'SELECT count(*) as count FROM photo.photo_thumb WHERE photo_id = "'+photo_id+'" AND size = "'+size+'"'
        conn.query(query, function(err, res){
            if(err)throw err;
            return callback(res);
        });
    });
}

module.exports = DataBase;

同样的问题也发生在插入查询上。 这是 node-mysql 包的问题还是我的代码的问题?

好吧,问题可能出在 node-mysql 包上,因为我在为 Node.js 使用了另一个 mysql 包后发现它似乎也比我原来的那个更受欢迎使用,更稳定。新软件包一切正常。我可能会在 github 上向 node-mysql 的作者提出这个问题以寻求解决。