[英]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.