gpt4 book ai didi

node.js - 在 node.js 和 mongodb 中处理异步数据库查询

转载 作者:可可西里 更新时间:2023-11-01 09:56:41 25 4
gpt4 key购买 nike

我在从 node.js 异步查询 mongodb 时遇到了这个问题。这是我的代码

var values = [];
var positives = new Array();
var negatives = new Array();

var server1 = new mongodb.Server('localhost',27017, {auto_reconnect: true});
var db1 = new mongodb.Db('clicker', server1);

db1.open(function(err, db) {
if(!err) {

db1.collection('feedback', function(err, collection) {
for (var i=0;i <5; i++) {
collection.find(
{value:1},
{created_on:
{
$gte:startTime + (i*60*1000 - 30*1000),
$lt: startTime + (i*60*1000 + 30*1000)
}
},
function(err_positive, result_positive) {
result_positive.count(function(err, count){
console.log("Total matches: " + count);
positives[i] = count;
});
}

);

collection.find(
{value:0},
{created_on:
{
$gte:startTime + (i*60*1000 - 30*1000),
$lt: startTime + (i*60*1000 + 30*1000)
}
},
function(err_negative, result_negative) {
result_negative.count(function(err, count){
console.log("Total matches: " + count);
negatives[i] = count;
});
}
);
}

});

} else {
console.log('Error connecting to the database');
}

});

实际上,我正在尝试从数据库中获取一些值。然后我需要操纵这些值。只是我需要从正数中减去负数,然后用正数-负数初始化值数组。现在,由于结果是异步获得的。我应该如何操作这些值并将它们放入值数组中。

最佳答案

在我进一步解释之前,我想指出您的代码中存在错误:

function(err_positive, result_positive) {
result_positive.count(function(err, count){
console.log("Total matches: " + count);
positives[i] = count; // <--- BUG: i id always 5 because it
}); // is captured in a closure
}

经典的闭包和循环问题。请参阅:Please explain the use of JavaScript closures in loops

现在,如何在循环中处理异步函数。基本思想是您需要跟踪完成了多少异步调用,并在最终调用返回后运行您的代码。例如:

var END=5;
var counter=end;
for (var i=0;i<END; i++) {
collection.find(
{value:1},
{created_on:
{
$gte:startTime + (i*60*1000 - 30*1000),
$lt: startTime + (i*60*1000 + 30*1000)
}
},
(function(j){
return function(err_positive, result_positive) {
result_positive.count(function(err, count){
console.log("Total matches: " + count);
positives[j] = count;
});

counter--;
if (!counter) {
/*
* Last result, now we have all positives.
*
* Add code that need to process the result here.
*
*/
}
}
})(i)
);
}

但是,如果我们继续这样做,很明显我们最终会创建一堆临时变量并最终得到可怕的嵌套代码。但这是 javascript,我们可以将此模式的逻辑封装在一个函数中。这是我在 javascript 中实现的这个“等待所有完成”逻辑:Coordinating parallel execution in node.js

但由于我们使用的是 node.js,我们可以使用方便的异步模块形式 npm:https://npmjs.org/package/async

使用异步,你可以这样写你的代码:

var queries = [];

// Build up queries:
for (var i=0;i <5; i++) {
queries.push((function(j){
return function(callback) {
collection.find(
{value:1},
{created_on:
{
$gte:startTime + (j*60*1000 - 30*1000),
$lt: startTime + (j*60*1000 + 30*1000)
}
},
function(err_positive, result_positive) {
result_positive.count(function(err, count){
console.log("Total matches: " + count);
positives[j] = count;
callback();
});
}

);
}
})(i));
queries.push((function(j){
return function(callback) {
collection.find(
{value:0},
{created_on:
{
$gte:startTime + (j*60*1000 - 30*1000),
$lt: startTime + (j*60*1000 + 30*1000)
}
},
function(err_negative, result_negative) {
result_negative.count(function(err, count){
console.log("Total matches: " + count);
negatives[j] = count;
callback();
});
}
);
}
})(i));
}

// Now execute the queries:
async.parallel(queries, function(){
// This function executes after all the queries have returned
// So we have access to the completed positives and negatives:

// For example, we can dump the arrays in Firebug:
console.log(positives,negatives);
});

关于node.js - 在 node.js 和 mongodb 中处理异步数据库查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13221262/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com