app.use(bodyParser.urlencoded({extended:true}))
mongoose.connect("mongodb://localhost:27017/shopDB",{useNewUrlParser:true,useUnifiedTopology: true});
app.get("/",function(req,res){
   var dbcollections;
   mongoose.connection.db.listCollections().toArray(function(err,names){
      if(err){
         console.log(err)
      }else{
         dbcollections = names;
         console.log(dbcollections) // first
      }
   });
   console.log(dbcollections) // second
   Item.find({},function(err,docs){
      if(err){
         console.log(err)
      }else{
        
        res.render("index", {list: docs ,collections: dbcollections});
      }
   }); 
   
})
The console prints as below:
undefined
[
  {
    name: 'items',
    type: 'collection',
    options: {},
    info: { readOnly: false, uuid: [Binary] },
    idIndex: { v: 2, key: [Object], name: '_id_' }
  }
]
It seemed that the second console.log(dbcollections) runs first thus return undefined. Why did this happen?
 
     
    