当前位置:网站首页>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
边栏推荐
- Bufferpool caching mechanism for executing SQL in MySQL
- Recursive least square adjustment
- Open 2022 efficient office, starting from project management
- MLX90614 driver, function introduction and PEC verification
- Tencent interview: can you find the number of 1 in binary?
- [source code] VB6 chat robot
- 2022.02.14
- SPI based on firmware library
- URLEncoder. Encode and urldecoder Decode processing URL
- Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
猜你喜欢
Exclusive download! Alibaba cloud native brings 10 + technical experts to bring "new possibilities of cloud native and cloud future"
How to make recv have a little temper?
I wrote a chat software with timeout connect function
Zipper table in data warehouse (compressed storage)
How to write a good title of 10w+?
Hcip day 15 notes
Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
Sort merge sort
SPI based on firmware library
X Opencv feature point detection and matching
随机推荐
Qtoolbutton available signal
2/14 (regular expression, sed streaming editor)
Tencent interview: can you pour water?
leetcode-43. String multiplication
2022 free examination questions for hoisting machinery command and hoisting machinery command theory examination
ADB command to get XML
Selenium check box
"Learning notes" recursive & recursive
[BSP video tutorial] stm32h7 video tutorial phase 5: MDK topic, system introduction to MDK debugging, AC5, AC6 compilers, RTE development environment and the role of various configuration items (2022-
Gossip about redis source code 81
QT creator source code learning note 05, how does the menu bar realize plug-in?
Go error collection | talk about the difference between the value type and pointer type of the method receiver
No qualifying bean of type ‘com. netflix. discovery. AbstractDiscoveryClientOptionalArgs<?>‘ available
SQL data update
Analysis of refrigeration and air conditioning equipment operation in 2022 and examination question bank of refrigeration and air conditioning equipment operation
[15th issue] Tencent PCG background development internship I, II and III (OC)
Idea integrates Microsoft TFs plug-in
Alibaba cloud container service differentiation SLO hybrid technology practice
How to quickly build high availability of service discovery
Esp-idf turns off serial port log output.