我使用的是node.js和本机mongodb驱动程序(node mongodb native);
我目前的项目使用node.js+now.js+mongo-db。
系统基本上将数据从浏览器发送到node.js,然后用haskell进行处理,然后再次反馈给浏览器。
通过一个表单和node.js,文本被插入到一个名为"messages"的mongodb集合中。haskell线程读取条目并将结果存储在数据库集合"results"中。这很好用。
但现在我需要等待结果出现在集合结果中的javascript代码。
伪代码:
wait until the collection result is non-empty.
findOne() from the collection results.
delete the collection results.
我目前连接到mongodb的方式如下:
var mongo = require('mongodb'),
Server = mongo.Server,
Db = mongo.Db;
var server = new Server('localhost', 27017, {
auto_reconnect: true
});
var db = new Db('test', server);
我的haskell知识很好,但不是我的javascript技能。所以我做了大量的搜索,但没有走多远。
很高兴你解决了这个问题,我本来打算写一些类似的东西:
setTimeout(function(){
db.collection('results',function(coll){
coll.findOne({}, function(err, one){
if( err ) return callback(err);
coll.drop(callback); //or destroy, not really sure <-- this will drop the whole collection
});
});
} ,1000);
解决方案是使用异步库。
var async = require('async');
globalCount = -1;
async.whilst(
function () {
return globalCount<1;
},
function (callback) {
console.log("inner while loop");
setTimeout(db_count(callback), 1000);
},
function (err) {
console.log(" || whilst loop finished!!");
}
);