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

∥☆過路亽.° 提交于 2019-12-13 15:18:50

问题


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

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

Example, for the following loop.

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);
    });
}

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;

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?


回答1:


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.



来源:https://stackoverflow.com/questions/36261011/not-getting-response-for-same-query-if-executed-more-than-10-times-in-same-scrip

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!