赞
踩
模板引擎书写中可能出现问题
注意查看传递到模板中的变量是否存在、变量的数据类型等
集合联合查询和渲染页面模板同时进行时会导致两者冲突,从而导致无法渲染页面
包含两种不同情况
错误代码
const id = req.query.id;
const article = await Arcticle.findOne({ _id: id }).populate("author");
res.render('./home/article', {
article: article
});
解决方法
使用lean方法将集合联合的结果转化为普通对象
const article = await Arcticle.findOne({ _id: id }).populate("author").lean();
错误代码
使用了第三方模块mongoose-sex-page实现分页功能
const pagination = require('mongoose-sex-page');
module.exports = async (req, res) => {
const articles = await pagination(Arcticle).find({}).page(1).size(4).display(5).populate('author').exec();
res.render('./home/default', {
articles: articles
});
}
解决方法
const articles = await pagination(Arcticle).find({}).page(1).size(4).display(5).populate('author').exec();
let string = JSON.stringify(articles);
let obj = JSON.parse(string);
res.render('./home/default', {
articles: obj
});
}
转自:
https://blog.csdn.net/weixin_50001396/article/details/112587981
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。