0

I'm assuming my array is empty because it takes time for the database to return the data that I'm needing and the function is running before data is being pushed to my array. But how can I wait for the data so that I can use it later on?

number.phoneNumber is working correctly so I'm assuming the push is working correctly.

MongoClient.connect(url, (err, client) => {
    var db = client.db('covid-poke-numbers');
    db.collection('phone-numbers').find().forEach(number => {
        phoneNumbers.push(number.phoneNumber);
    })
    client.close();
});

consoleLogNumbers();

function consoleLogNumbers() {
    console.log(phoneNumbers);
}
uster1
  • 13
  • 3

0 Answers0