Mongo find() returns [object 对象] in express
Mongo find() returns [object Object] in express
所以基本上我正在尝试从 header.ejs
访问页面集合
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 访问?
Pages.find()
by default will return a cursor. You'll want to use a callback 处理您发现的内容:
app.use(function(req, res, next) {
Pages.find({}, (err, pages) => {
if (err) return next(err);
res.locales.pages = pages;
return next();
});
});
- 查看
Converting an object to a string 获取有关如何将对象或数组转换为
string
的信息。默认情况下,当您将对象转换为字符串时,它将显示为 [object Object]
(您可以通过执行 const a = {}; console.log({}.toString())
) 来验证这一点
所以基本上我正在尝试从 header.ejs
访问页面集合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 访问?
Pages.find()
by default will return a cursor. You'll want to use a callback 处理您发现的内容:
app.use(function(req, res, next) {
Pages.find({}, (err, pages) => {
if (err) return next(err);
res.locales.pages = pages;
return next();
});
});
- 查看
Converting an object to a string 获取有关如何将对象或数组转换为
string
的信息。默认情况下,当您将对象转换为字符串时,它将显示为[object Object]
(您可以通过执行const a = {}; console.log({}.toString())
) 来验证这一点