0

I'm in front of a huge problem that I cannot solve. I want to get the result of the function collection.find() of Mongodb then put the result in a variable that I could reuse in another function running independently. Because it's a little thorough to explain, here's the code:

client.connect(function (err) {
    assert.equal(null, err);
    const db = client.db(myDatabase);
    const collection = db.collection(myCollection);
    collection.find({}).toArray(function (err, docs) {
        //docs is the final result that I want to store in a variable
    });
});

$(myInput).change(function() {
    //using docs
})

'docs' being the result of a callback, I don't know how to retrieve it in a variable. I tried to store the whole thing in a variable, I tried global variables but nothing works, I still get undefined each time I run my program. So yes, I could run my function into the callback of collection.find()

client.connect(function (err) {
        assert.equal(null, err);
        const db = client.db(myDatabase);
        const collection = db.collection(myCollection);
        collection.find({}).toArray(function (err, docs) {
            $(myInput).change(function(docs) {
                //using docs
            })
        });
});

but since it's a function which I run a lot of time, it would call Mongo very often and that's not the best thing for performance especially since my database is running on another computer.

palmtreesnative
  • 2,835
  • 3
  • 14
  • 22
  • It's async, hence you need to call `myFunction` **inside** the .toArray callback, and pass `docs` as a function parameter (or make it global, but that wouldn't make much sense). Can you please share where myFunction is called? – briosheje Oct 20 '18 at 16:37
  • I got clumsy when I write my question. myFunction is running independently and do things with docs. – palmtreesnative Oct 20 '18 at 16:57
  • Can you please share the full code then? **where is myFunctions called?** – briosheje Oct 20 '18 at 17:01
  • Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – ghybs Oct 20 '18 at 17:07
  • Yes I'm going to – palmtreesnative Oct 20 '18 at 17:08

2 Answers2

2

Define docs in the global scope and assign the data after the find method

let docs;

client.connect(async err => {
  assert.equal(null, err);
  const db = client.db(myDatabase);
  const collection = db.collection(myCollection);
  try {
    // docs is now a global variable containing all of the db collection
    docs = await collection.find({});
    myFunction();
  } catch (error) {
    // do something with the error
  }
});

function myFunction(){
  console.log("print docs", docs)
}
Asaf Aviv
  • 11,279
  • 1
  • 28
  • 45
  • myFunction is running independently which I forgot to mention. Put it in the callback could be a solution but I'm trying to do it differently (store docs in a global variable that could be reused independently in myFunction) – palmtreesnative Oct 20 '18 at 17:00
  • Do you wanna call `myFunction` from other files ? – Asaf Aviv Oct 20 '18 at 17:04
  • No, only this one (I'm calling these from a script other than main.js) – palmtreesnative Oct 20 '18 at 17:06
  • Why is my solution dosen't work for you ? docs is now a global variable containing the `collection.find` data which you can do whatever you want with it.. i think you should explain what you are trying to do thats failing – Asaf Aviv Oct 20 '18 at 17:10
  • I finally got your solution to work (mistake from me). Thank you a lot. I just wanted not to run myFunction in the callback but since the variable is now global I can use it independently. – palmtreesnative Oct 20 '18 at 17:17
  • Nice, glad to help. – Asaf Aviv Oct 20 '18 at 17:19
0

Mongo already uses promises for async too (assuming v3+)

Let doc=[];

client.connect(function (err) {
assert.equal(null, err);
const db = client.db(myDatabase);
Let collection=               db.collection(myCollection);
collection.find({})
.then(results=>{
  //modify data
  doc.push(results);
  myfunction();
 }

});

 function  myFunction(){
console.log(doc)
  }
Pari Baker
  • 696
  • 4
  • 19