Sunday, 9 April 2017

Node.js - Fetch all data from mongoDB and show it as list in views(index.js)

Schema

var ItemSchema = mongoose.Schema({
username: {
  type: String,
  index: true
},
 path: {
   type: String
 },
 originalname: {
   type: String
 }
});

var Item = module.exports = mongoose.model('Item',ItemSchema, 'iteminfo');

route

router.get('/', ensureAuthenticated, function(req, res){
  Item.find({},function(err, docs){
                res.render('welcome', {docs:docs});
        });
});

index.js

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title></title>
  </head>
  <body>
    [list of data here!!!]
  </body>
</html>

Is my code in routes correct? how to display all data in index.js? Help please. I am a newbie in node.js and mongoDB. Thanks :)



via Lawrence Ricafort Bacus

No comments:

Post a Comment