I simply want to return the collection from the mongo db to use it in an other part of the program.
I assume that I have to use call back functions. I tried many times but it never works. The return gets executed before the db has completed.
function getAllJobs() {
var MongoClient = mongo.MongoClient;
var url = 'mongodb://localhost:27017/uniyapp';
MongoClient.connect(url, function(err, db) {
queryCollection`enter code here`(db.collection('jobs'), db, function(result, db){
console.log(result);
//You can do more stuff with the result here
return result
});
});
Here the function for finding the collection:
function queryCollection(collection, db, callback){
collection.find().toArray(function(err, result) {
if (err) {
console.log(err);
db.close();
} else if (result.length > 0) {
callback(result, db);
db.close();
} else {
db.close();
}
});
}
It would help me so much if I could solve this problem. Thanks in advance!
via dermaschder
No comments:
Post a Comment