Error information
Error code
let articles = await Article.find().populate('author');
res.render('admin/article', { articles });
Error reason
When the populate </ code> method is used in mongoose to realize the set association, the template engine cannot render. This is because when the set joint query and rendering page template are carried out at the same time, the two conflicts will result in the failure of rendering the page. For example, we change the above code to the following:
let articles = await Article.find().populate('author');
res.send('ok'); // The server side can respond to the client ok so there is a real conflict between template rendering and federated queries
resolvent
Using the lean () method
let articles = await Article.find().populate('author').lean();
res.render('admin/article', { articles });
Lean() method: it tells mongoose to return a normal object instead of a mongoose document object, which is used internally first JSON.stringify () this method converts the document object to a string, removes all other attribute formats, and then leaves only the required data string~
Using stringfy() and parse() methods
And the above method is essentially the same
let articles = await Article.find().populate('author');
let str = JSON.stringify(result);
let json = JSON.parse(str);
res.render('admin/article', { json });
When we continue the paging function, we use the JSON method
To achieve paging function, we use the mongoose sex page </ code> third-party module for paging, and then we change the code to:
let temp = await pagination(Article).find({})
.page(page)
.size(2)
.display(2).populate('author')
.exec();
let str = JSON.stringify(temp);
let articles = JSON.parse(str);
res.render('admin/article', { articles });
The problem will be solved π
Read More:
- vue VM682:1 Uncaught SyntaxError: Unexpected token u in JSON at position 0
- [Solved] SyntaxError:JSON.parse:unexpected character at line 1 column 1 of the JSON data
- R: Data frame index error “unexpected token”
- Refresh 404 after packaging Vue project, uncaught syntax error: unexpected token οΌ
- [Solved] Parsing JSON error unexpected end of JSON input
- [Solved] SyntaxError: Unexpected end of JSON input
- How to Solve Error: “syntax error near unexpected token `newline’”
- [Solved] umi Project Create Error: Rendered more hooks or Rendered fewer hooks
- [Solved] Vscode Katex Error: ParseError: KaTeX Parse Error: Canβt Use Function β$β In Math Mode At Position …
- [Solved] thymeleaf.TemplateEngineException processing template βmainβ: An error happened during template pars
- JSON parse error: Can not deserialize instance of java.lang.String out of START_OBJECT token
- [Soled] Eslint Error: Unexpected literal in error position of callback
- Webpack Pack and compress ES6 files with errors: ERROR in js/xxxxxx.js from UglifyJs Unexpected token punc ()
- How to parse JSON string in.Net [error reading job object from jsonreader. Current jsonreader item is not an obj]
- Parsing error in eslint parsing: unexpected token [How to Solve]
- [Solved] A needed class was not found. This could be due to an error in your runpath. Missing class: scala/co
- Regarding Pyhton regular error: sre_constants.error: nothing to repeat at position
- [Solved] JSON parse error: Unexpected character (βββ (code 39)): was expecting double-quote to start ……
- [Solved] JSON parse error: Unexpected character (βββ (code 39)): was expecting double-quote to star
- Vue Error compiling template: Component template should contain exactly one root element. If you