当前位置:网站首页>Private project practice sharing populate joint query in mongoose makes the template unable to render - solve the error message: syntaxerror: unexpected token r in JSON at
Private project practice sharing populate joint query in mongoose makes the template unable to render - solve the error message: syntaxerror: unexpected token r in JSON at
2022-07-03 23:46:00 【That's a simple man】
Error message

Error code
let
articles
=
await
Article.
find().
populate(
'author');
res.
render(
'admin/article', {
articles });
- 1.
- 2.
Cause of error
stay mongoose Use in populate Method to implement Collection Association , Causes the template engine to fail to render , This is because when the collection jointly queries and renders the page template at the same time, they will conflict , As a result, the page cannot be rendered , For example, let's change the above code to something like this :
let
articles
=
await
Article.
find().
populate(
'author');
res.
send(
'ok');
// The server can respond to the client ok So template rendering does conflict with federated queries
- 1.
- 2.
resolvent
Use lean() Method
let
articles
=
await
Article.
find().
populate(
'author').
lean();
res.
render(
'admin/article', {
articles });
- 1.
- 2.
lean() Method : Is to tell mongoose Returns a normal object , instead of mongoose Document object , Method internal first use JSON.stringify() This method converts the document object to a string , Remove all other attribute formats , Then leave only the data string you need ~
Use stringfy() and parse() Method
It's essentially the same as the above method .
let
articles
=
await
Article.
find().
populate(
'author');
let
str
=
JSON.
stringify(
result);
let
json
=
JSON.
parse(
str);
res.
render(
'admin/article', {
json });
- 1.
- 2.
- 3.
- 4.
While continuing with paging , We use JSON Method
When it comes to paging , We used mongoose-sex-page Third party module pagination , Then we changed 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 });
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
The problem is solved !
Reference blog https://blog.csdn.net/qq_49002903/article/details/112541719
边栏推荐
- QT creator source code learning note 05, how does the menu bar realize plug-in?
- Bufferpool caching mechanism for executing SQL in MySQL
- Recursive least square adjustment
- Scratch uses runner Py run or debug crawler
- "Learning notes" recursive & recursive
- Sword finger offer day 4 (Sword finger offer 03. duplicate numbers in the array, sword finger offer 53 - I. find the number I in the sorted array, and the missing numbers in sword finger offer 53 - ii
- The first game of the new year, many bug awards submitted
- Analysis of refrigeration and air conditioning equipment operation in 2022 and examination question bank of refrigeration and air conditioning equipment operation
- EPF: a fuzzy testing framework for network protocols based on evolution, protocol awareness and coverage guidance
- I would like to ask how the top ten securities firms open accounts? Is it safe to open an account online?
猜你喜欢

leetcode-43. String multiplication

Idea set class header comments

Common mode interference of EMC

Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?

2022 chemical automation control instrument examination content and chemical automation control instrument simulation examination

MLX90614 driver, function introduction and PEC verification

2022 free examination questions for hoisting machinery command and hoisting machinery command theory examination
![[Happy Valentine's day]](/img/d9/9280398eb64907a567df6eea772adb.jpg)
[Happy Valentine's day] "I still like you very much, like sin ² a+cos ² A consistent "(white code in the attached table)

JDBC Technology

QT creator source code learning note 05, how does the menu bar realize plug-in?
随机推荐
"Learning notes" recursive & recursive
Analysis on the scale of China's smart health industry and prediction report on the investment trend of the 14th five year plan 2022-2028 Edition
2022 chemical automation control instrument examination content and chemical automation control instrument simulation examination
Powerful blog summary
Idea a method for starting multiple instances of a service
Les sociétés de valeurs mobilières dont la Commission d'ouverture d'un compte d'actions est la plus faible ont ce que tout le monde recommande.
I would like to ask how the top ten securities firms open accounts? Is it safe to open an account online?
Briefly understand the operation mode of developing NFT platform
"Learning notes" recursive & recursive
Unsafe and CAS principle
How to understand the gain bandwidth product operational amplifier gain
How to quickly build high availability of service discovery
FPGA tutorial and Allegro tutorial - link
A preliminary study on the middleware of script Downloader
Interesting 10 CMD commands
Gossip about redis source code 73
Gossip about redis source code 80
2022 t elevator repair registration examination and the latest analysis of T elevator repair
股票开户佣金最低的券商有哪些大家推荐一下,手机上开户安全吗
Unity shader visualizer shader graph