Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
节点js调用函数,多次访问mysql数据库并返回json结果_Mysql_Json_Node.js - Fatal编程技术网

节点js调用函数,多次访问mysql数据库并返回json结果

节点js调用函数,多次访问mysql数据库并返回json结果,mysql,json,node.js,Mysql,Json,Node.js,我是Node.js的新手。我有一个函数“getFromDb”,它访问mysql数据库并返回一个包含一些数据的json文件。如果我有一个查询数据数组,并且我想通过for循环调用同一个函数来获取数组中每个元素的json文件,该怎么办 var http = require('http'); http.createServer(function(req, res) { console.log('Receving request

我是Node.js的新手。我有一个函数“getFromDb”,它访问mysql数据库并返回一个包含一些数据的json文件。如果我有一个查询数据数组,并且我想通过for循环调用同一个函数来获取数组中每个元素的json文件,该怎么办

        var http = require('http');         

        http.createServer(function(req, res) {

            console.log('Receving request...');

            var callback = function(err, result) {
                res.setHeader('Content-disposition', 'attachment; filename=' + queryData+ '.json');
                res.writeHead(200, {
                    'Content-Type' : 'x-application/json'
                });
                console.log('json:', result);
                res.end(result);    
            };

        getFromDb(callback, queryData);}  
        ).listen(9999);

        function getFromDb(callback, queryData){
        var mysql = require('mysql');
            var connection = mysql.createConnection({
                host : 'localhost',
                user : 'xxxx',
                password : 'xxxx',
                database : 'xxxx',
                port: 3306
            });

            connection.connect();
            var json = '';
            var data = queryData + '%';
            var query = 'SELECT * FROM TABLE WHERE POSTCODE LIKE "' + data + '"';
            connection.query(query, function(err, results, fields) {
                if (err)
                    return callback(err, null);

                console.log('The query-result is: ', results);

                // wrap result-set as json
                json = JSON.stringify(results);

                /***************
                * Correction 2: Nest the callback correctly!
                ***************/
                connection.end();
                console.log('JSON-result:', json);
                callback(null, json);
            });
        }

为此,可以使用节点的异步库。该库具有许多函数,使NodeJS中的异步编程变得更加容易。“each”或“eachSeries”函数可以工作。“each”将一次对mysql进行所有调用,“eachSeries”将等待上一次调用完成。您可以在数组的getFromDB方法中使用它

见:


为此,可以使用节点的异步库。该库具有许多函数,使NodeJS中的异步编程变得更加容易。“each”或“eachSeries”函数可以工作。“each”将一次对mysql进行所有调用,“eachSeries”将等待上一次调用完成。您可以在数组的getFromDB方法中使用它

见:


为此,可以使用节点的异步库。该库具有许多函数,使NodeJS中的异步编程变得更加容易。“each”或“eachSeries”函数可以工作。“each”将一次对mysql进行所有调用,“eachSeries”将等待上一次调用完成。您可以在数组的getFromDB方法中使用它

见:


为此,可以使用节点的异步库。该库具有许多函数,使NodeJS中的异步编程变得更加容易。“each”或“eachSeries”函数可以工作。“each”将一次对mysql进行所有调用,“eachSeries”将等待上一次调用完成。您可以在数组的getFromDB方法中使用它

见:


使用异步模块。这是最好的。如果您不想添加新模块,请尝试以下操作

var count = 0;

array.forEach(function(element) { //array of the data that is to be used to call mysql
    ++count; //increase counter for each service call
    async.db.call(element, callback); //the async task
}
var data = [];
function callback(err, resp) {
    --count;//subtract for each completion
    data.push(resp)
    if(count == 0) { //return data when all is complete
        return data;
    }
}

不过我还是建议使用异步模块。这是一个非常好的实践,非常有用。

使用异步模块。这是最好的。如果您不想添加新模块,请尝试以下操作

var count = 0;

array.forEach(function(element) { //array of the data that is to be used to call mysql
    ++count; //increase counter for each service call
    async.db.call(element, callback); //the async task
}
var data = [];
function callback(err, resp) {
    --count;//subtract for each completion
    data.push(resp)
    if(count == 0) { //return data when all is complete
        return data;
    }
}

不过我还是建议使用异步模块。这是一个非常好的实践,非常有用。

使用异步模块。这是最好的。如果您不想添加新模块,请尝试以下操作

var count = 0;

array.forEach(function(element) { //array of the data that is to be used to call mysql
    ++count; //increase counter for each service call
    async.db.call(element, callback); //the async task
}
var data = [];
function callback(err, resp) {
    --count;//subtract for each completion
    data.push(resp)
    if(count == 0) { //return data when all is complete
        return data;
    }
}

不过我还是建议使用异步模块。这是一个非常好的实践,非常有用。

使用异步模块。这是最好的。如果您不想添加新模块,请尝试以下操作

var count = 0;

array.forEach(function(element) { //array of the data that is to be used to call mysql
    ++count; //increase counter for each service call
    async.db.call(element, callback); //the async task
}
var data = [];
function callback(err, resp) {
    --count;//subtract for each completion
    data.push(resp)
    if(count == 0) { //return data when all is complete
        return data;
    }
}

不过我还是建议使用异步模块。这是一个非常好的实践,非常有用。

所以您认为我应该使用如下内容:var arrayOfQueryData=new Array();arrayOfQueryData=[“查询1”、“查询2”、“查询3”、“查询4”、“查询5”];异步=需要(“异步”);each(arrayOfQueryData,getFromDb,function(err){console.log(err);});在getFromDb函数中?我不喜欢发布未经测试的代码,但这应该会让您产生想法;arrayOfQueryData=[“查询1”、“查询2”、“查询3”、“查询4”、“查询5”];异步=需要(“异步”);each(arrayOfQueryData,getFromDb,function(err){console.log(err);});在getFromDb函数中?我不喜欢发布未经测试的代码,但这应该会让您产生想法;arrayOfQueryData=[“查询1”、“查询2”、“查询3”、“查询4”、“查询5”];异步=需要(“异步”);each(arrayOfQueryData,getFromDb,function(err){console.log(err);});在getFromDb函数中?我不喜欢发布未经测试的代码,但这应该会让您产生想法;arrayOfQueryData=[“查询1”、“查询2”、“查询3”、“查询4”、“查询5”];异步=需要(“异步”);each(arrayOfQueryData,getFromDb,function(err){console.log(err);});在getFromDb函数中?我不喜欢发布未经测试的代码,但这应该会给你一个想法。你能给我更多关于使用异步模块的提示,与问题中给出的代码相关吗?你能给我更多关于使用异步模块的提示,与问题中给出的代码相关吗?你能给我更多关于使用异步模块的提示吗,关于问题中给出的代码,请?您能给我更多关于使用异步模块的提示吗,关于问题中给出的代码,请?