相关疑难解决方法(0)

node.js,express - 以同步方式在循环内一个接一个地执行mysql查询

在我的node.js,表达应用程序,我正在与superagent中间件进行ajax调用.该调用使用node-mysql中间件通过相当多的数据库查询来获取复杂数组中的数据库数据.

在粘贴代码之前,我试图用语言解释我想要做什么,尽管代码足以说明它想要做什么,并且第一次回调中的所有异步事件应该以同步方式完成.

说明:

在第一个查询的回调中,执行for循环以多次运行第二个查询,并且在每个循环之后,仅在第二个查询的回调完成后才调用下一个循环.下一个代码行的情况也是一样的.

码:

但是,您可以跳过 for循环的内部(在注释中标记),以便在需要时简化和简化.

conn.query("SELECT * FROM `super_cats`",function(error, results, fields) {   

        if(error){console.log("erro while fetching products for homepage "+ error);}

        for(var i in results) { // FIRST FOR LOOP INSIDE THE FIRST QUERY CALLBACK

            /*Innards of for loop starts*/    
            var elem = new Object();
            var supcat_id=results[i].id;
            elem.super_id =supcat_id;
            elem.cats=new Array();
            var cat= '';
            /*Innards of for loop ends*/ 

            conn.query("SELECT * FROM `categories` WHERE `supcat_id`="+supcat_id,function(error_cats, results_cats, fields_cats) {   

                if (error_cats) …
Run Code Online (Sandbox Code Playgroud)

asynchronous node.js express node-mysql

8
推荐指数
1
解决办法
8256
查看次数

在async.series中调用async.series会产生不可预测的输出

使用caolan的node.js异步库,我一直在尝试调用一个async.series在另一个使用async.series的函数内部使用的函数,但我仍然无法使函数以正确的顺序运行,详情如下:

终端输出显示第一个函数在第一个函数之前被调用,原因不明:

The "sys" module is now called "util". It should have a similar interface.
Starting the second step in the series
Value of b: undefined
Invoking the function firstStep
the value of toObtain is: [object Object]
Run Code Online (Sandbox Code Playgroud)

这是相应的源代码:

var im = require('imagemagick');
var async = require('async');

var toObtain;


var b;
async.series([

function (callback) {
    //It appears that this function is being invoked after the second function.
    //Why is this happening?
    firstStep();
    callback();
},

function (callback) {
    //Why is …
Run Code Online (Sandbox Code Playgroud)

node.js

4
推荐指数
1
解决办法
3217
查看次数

标签 统计

node.js ×2

asynchronous ×1

express ×1

node-mysql ×1