如果使用node-mysql在同一脚本中执行了10次以上,则无法获得相同查询的响应 [英] Not getting response for same query if executed more than 10 times in same script using node-mysql

查看:79
本文介绍了如果使用node-mysql在同一脚本中执行了10次以上,则无法获得相同查询的响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几小时前,我问了这个问题

Few hours back I asked this question. I was troubleshooting it when I Zeroed in on this current issue.

这里是循环,如果我执行函数selectPhotoById超过10次,则仅获得10个查询的响应.

Here is the loop, if I execute the function selectPhotoById more than 10 times, I get response for only 10 queries.

示例,用于以下循环.

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

在控制台响应中,我得到

in the console response, I get

12246
12246
12246
12246

这很好,但是如果我增加循环次数

This works fine, but if i increase the loop like

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

我只有10个ID作为响应

I get only 10 IDs as response

12246
12246
12246
12246
12246
12246
12246
12246
12246
12246

这是写查询的文件的代码

Here is the code of the file where the queries are written

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 包出现的问题还是我的代码出现了问题?

The same issue is happening for the insert query as well. Is this an issue with the node-mysql package or the issue is with my code?

推荐答案

好吧,问题可能出在使用另一个

Well, the issue probably was with the node-mysql package as I found out after using another mysql package for Node.js which also seems to be more popular than the one I was using, and more stable. Everything's working fine with the new package. I'll probably put up this issue with the Author of node-mysql on github for resolution.

这篇关于如果使用node-mysql在同一脚本中执行了10次以上,则无法获得相同查询的响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆