B
B
Banan442020-05-12 12:52:39
MongoDB
Banan44, 2020-05-12 12:52:39

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

How to get data from collection?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dima Polos, 2020-05-12
@Banan44

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 question

Ask a Question

731 491 924 answers to any question