Mongo find()以快递形式返回[object Object]

时间:2019-03-06 21:55:00

标签: mongodb express mongoose schema mongoose-schema

所以基本上我正在尝试从header.ejs访问Pages集合

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

但是当我尝试从html访问它时,它显示[object object]

当我在console.log上显示它时,会显示很多

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: ....................

如何通过数组从html访问?

1 个答案:

答案 0 :(得分:1)

  1. Pages.find()默认情况下将返回光标。您将要使用callback处理发现的内容:
app.use(function(req, res, next) {
  Pages.find({}, (err, pages) => {
    if (err) return next(err);
    res.locales.pages = pages;
    return next();
  });
});
  1. 退房 Converting an object to a string,以获取有关如何将对象或数组转换为string的信息。默认情况下,将对象转换为字符串时,它将读取[object Object](您可以通过执行const a = {}; console.log({}.toString())进行验证)