一、SyntaxError: Unexpected token R in JSON at position 0
错误信息截图:
错误位置代码
//查询所有文章数据
let articles = await Article.find().populate("author");
在mongoose中使用populate方法实现集合关联时,导致模板引擎art-template无法渲染
原文链接:详细解释
**解决方法:**后面添加.lean()
//查询所有文章数据
let articles = await Article.find().populate("author").lean();
修改完以后,关于R的错误没有了,但是接下来的代码中又出现了新的错误
lean is not a function,所以要把lean()去掉,就用到了原文链接里的第二中方法
//将文章集合的构造函数导入到当前文件中
const { Article } = require("../../model/article");
//导入mongoose-sex-page模块
const pagination = require("mongoose-sex-page");
module.exports = async (req, res) => {
//标识 表示当前访问的是文章管理页面
req.app.locals.currentLink = "article";
//查询所有文章数据
let articles = await pagination(Article)
.find()
.page(1) //指定当前页
.size(2) //指定每页显示的数据条数
.display(3) //display 指定客户端要显示的页码数
.populate("author")
.exec();
let str = JSON.stringify(articles);
let json = JSON.parse(str);
//渲染文章列表页面模板
res.render("admin/article.art", {
articles: json,
});
};
二、SyntaxError: Unexpected token T in JSON at position 0
错误信息截图:
经查证,是模板语句有问题
原文链接:查证来源
挨个查找发现,确实漏了一个{{/block}}
PS:一个不小心的粗心大意,浪费一段没必要的时间