I am currently able to run .find().forEach(function(result))
and use console.log
to see what was returned from my MongoDB server using Node.js
. Yet when I attempt to do array.push(result.name)
the array is not updated. Below is the script that I am using:
// Create temp array to store orders
var orderArray = [];
// Connect to Orders
MongoClient.connect('mongodb://10.254.17.115:27017/Orders', function(err, db) {
// Handle errors
assert.equal(null, err);
var found = db.collection('Orders').find().forEach(function (result) {
orderArray.push(result.OrderID);
console.log(result.OrderID);
});
//socket.emit('GetOrders', orderArray);
});
// Return order array
console.log("Array used for debug " + orderArray);
I would assume that the script I used would work since the scope of orderArray[] is able to reach inside and outside the .find()
function. Would anybody know a fix for this?