-1

so basically i'm trying to access Pages collection from header.ejs

app.use(function(req,res,next){
    res.locals.pages= Pages.find();
    next();
});

but when i try to access it from the html it display [object object]

and when i display it on console.log it shows a lot

createIndexes instead.
Query {
  _mongooseOptions: {},
  _transforms: [],
  _hooks: Kareem { _pres: Map {}, _posts: Map {} },
  _executionCount: 0,
  mongooseCollection: 
   NativeCollection {
     collection: Collection { s: [Object] },
     opts: 
      { bufferCommands: true,
        capped: false,
        '$wasForceClosed': undefined },
     name: 'pages',
     collectionName: 'pages',
     conn: ....................

how can i access from html by array ?

Jack
  • 65
  • 1
  • 1
  • 5

1 Answers1

2
  1. Pages.find() by default will return a cursor. You'll want to use a callback to handle what you've found:
app.use(function(req, res, next) {
  Pages.find({}, (err, pages) => {
    if (err) return next(err);
    res.locales.pages = pages;
    return next();
  });
});
  1. Check out Converting an object to a string for info on how to convert an object or array to a string. By default, when you convert an object to a string, it will read [object Object] (you can verify this by doing const a = {}; console.log({}.toString()))
klhr
  • 3,300
  • 1
  • 11
  • 25