Answer the question
In order to leave comments, you need to log in
Why Undefined?
let Categories
mongoClient.connect((error, client) => {
if(error) throw error
let db = client.db('Project')
let categories = db.collection('categories')
categories.find().toArray((error, results) => {
Categories = results
console.log(Categories) // Все ок, выводит
client.close()
})
})
console.log(Categories) // undefined
Answer the question
In order to leave comments, you need to log in
The idea is undefined, because you read to the console synchronously, and the variable in the callback is written asynchronously.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question