vendredi 20 février 2015

Node, mongo and loop, how to break loop when I find data

My code looks similar to that:



var mongo_client = require('mongodb').MongoClient, dataStorage;
lib = {
[...]
find: function(res, param, callback) {
var parentPath = param.path;

while (parentPath !== '/') {
collection.findOne({'paths' : parentPath}, {...}, function(err, data)) {
if (data) {
dataStorage = data;
callback(data, res);
}
}
if (dataStorage) {
return;
}
parentPath = lib.removeLastBlockOfPath(parentPath);
}
if (!dataStorage) {
callback(someDefaultData, res);
}
}
[...]
}


What I want to do is to find some path stored in mongo, or if there is no match, try do find first matching parent path.


I can't set dataStorage value from findOne callback is it any way to do that? Eaven if I find path it always run thru all path blocks.


Aucun commentaire:

Enregistrer un commentaire