简体   繁体   English

node js:使用相同的参数名在另一个函数内执行一个函数

[英]node js : execute one function inside another with same parameter names

I have a beginners question regarding node.js / javascript syntax and asynchronous calls. 我有一个关于node.js / javascript语法和异步调用的初学者问题。 I'm using node-dbi to pull some information out of a MySQL Server. 我正在使用node-dbi从MySQL服务器中提取一些信息。

I have 2 tables. 我有2张桌子。 Client and Zone . 客户区域

I need to write a function that does this: 我需要编写一个执行此操作的函数:

for (i=0;<zone.count;i++){
  for (j=0;j<client.count;j++){
     //loop through the entire client table for each zone record & run a condition
  }
}

Here is what my syntax in node-dbi looks like: 这是我在node-dbi中的语法:

db.fetchAll('SELECT * from Zone', null, function(err, result){
    if (result) {
        db.fetchAll('SELECT * from Client', null, function(err, result){
            if (result) {
                //do something to all client records for each zone record
            }
        });
    }
});

As it's immediately obvious, my result and err variables are clashing.. Can someone please explain the syntax to solve this asynchronous function? 因为它很明显,我的结果和错误的变量是冲突的..有人可以解释一下语法来解决这个异步函数吗?

Thanks! 谢谢!

Give each variable a name specific to that function: 为每个变量指定一个特定于该函数的名称:

db.fetchAll('SELECT * from Zone', null, function(zoneErr, zoneResult){
    if (zoneResult) {
        db.fetchAll('SELECT * from Client', null, function(clientErr, clientResult){
            if (clientResult) {
                //do something to all client records for each zone record
            }
        });
    }
});

Also, you should refactor that a bit to make it more readable and remove that deep nesting. 此外,您应该重构一下以使其更具可读性并删除深度嵌套。

Here's one way to think about it: 这是一种思考方式:

var getZones = function() {
  var result;
  db.fetchAll('SELECT * from Zone', null, function(zoneErr, zoneResult){
     if (zoneResult) {
       callback();
       result = true; // assumes you need to keep track of success of failure of result
     } else {
       result = false;
     }
  }
  return result;
};


var getClients { ... same code as above for clients };

getZones(getClients);

But that refactoring is off the cuff - it may not fit in your situation. 但是重构不在袖口 - 它可能不适合你的情况。

The problem should be solved by the first piece of code 问题应该通过第一段代码来解决

Either rename the err and result into errZone,resultZone /errClient,resultClient as Squadrons suggested, or use async https://npmjs.org/package/async 将err和结果重命名为errZone,resultZone / errClient,resultClient作为Squadrons建议,或者使用async https://npmjs.org/package/async

// WARNING UNTESTED CODE
// npm install async
// or add async into your package.json dependencies
var async = require('async');
async.parallel({ 
        zone: function (callback) {
            db.fetchAll('SELECT * from Zone', null, function (err, result) {
                if (err || !result) {
                    callback(err || 'No Zone results found', result)
                }
            });
        },
        client: function (callback) {
            db.fetchAll('SELECT * from Client', null, function (err, result) {
                if (err || !result) {
                    callback(err || 'No Client results found', result)
                }    
            });
        }
    },
    function (err, results) {
        if (err) throw err;
        for (var i = 0; i < results.zone.count; i++) {
            for (var j = 0; j < results.client.count; j++) {
                //loop through the entire client table for each zone record & run a     condition
            }
        }
    });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何依次执行节点js函数,这些函数使用它们的名称在字符串数组中动态列出 - How to execute node js functions one after another which dynamically listed using their names in a string array 如何将参数从一个页面 function 传递到另一个页面 class function 在 Z9E13B69D1D715A927102ACAAAF14Z - how to pass parameter from one page function to another page class function in Javascript / Node js 使用参数将 function 插入另一个内部 - Insert function inside another one by using parameter 函数参数无法在Node.JS Jasmine测试中执行 - Function parameter failing to execute in Node.JS Jasmine test 另一个函数中的函数不会在 JS 中执行 - function inside another function doesn't execute in JS 如何在另一个内部具有onload函数的函数执行? - How to execute a function after another one that has an onload function inside it? 如何在Node JS中的异步函数中顺序执行代码 - How to execute code sequentially inside asynchronous function in node js 如何在node.js 4.2.3中执行具有不同超时的一个功能 - How to execute one function with different timeouts in node.js 4.2.3 节点js在循环节点js中完成多项功能后执行另一个功能 - Node js execute another function after complete multiple function in loop node js 如何将node.js模块/源合并到一个.js文件中,以在节点内部执行 - How to combine node.js modules/source into one .js file, to execute inside node
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM