当前位置:网站首页>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
边栏推荐
- 股票开户最低佣金炒股开户免费,网上开户安全吗
- China standard gas market prospect investment and development feasibility study report 2022-2028
- D24:divisor and multiple (divisor and multiple, translation + solution)
- Maxwell equation and Euler formula - link
- Kubedl hostnetwork: accelerating the efficiency of distributed training communication
- Loop compensation - explanation and calculation of first-order, second-order and op amp compensation
- 2.14 summary
- Deep learning ----- using NN, CNN, RNN neural network to realize MNIST data set processing
- Scratch uses runner Py run or debug crawler
- Ramble 72 of redis source code
猜你喜欢
Sort merge sort
How to make recv have a little temper?
Alibaba cloud container service differentiation SLO hybrid technology practice
Correlation analysis summary
The interviewer's biggest lie to deceive you, bypassing three years of less struggle
leetcode-43. String multiplication
2022 chemical automation control instrument examination content and chemical automation control instrument simulation examination
Yyds dry goods inventory [practical] simply encapsulate JS cycle with FP idea~
Fluent learning (5) GridView
[network security] what is emergency response? What indicators should you pay attention to in emergency response?
随机推荐
What is the difference between NFT, SFT and dnft? How to build NFT platform applications?
I would like to ask how the top ten securities firms open accounts? Is it safe to open an account online?
How to make recv have a little temper?
Selenium check box
[network security] what is emergency response? What indicators should you pay attention to in emergency response?
Scratch uses runner Py run or debug crawler
China standard gas market prospect investment and development feasibility study report 2022-2028
Open 2022 efficient office, starting from project management
Selenium library 4.5.0 keyword explanation (4)
Op amp related - link
2022 chemical automation control instrument examination content and chemical automation control instrument simulation examination
Tencent interview: can you pour water?
Learning methods of zynq
Hcip day 14 notes
Bufferpool caching mechanism for executing SQL in MySQL
Cgb2201 preparatory class evening self-study and lecture content
Errors taken 1 Position1 argument but 2 were given in Mockingbird
2022 t elevator repair registration examination and the latest analysis of T elevator repair
Make small tip
[source code] VB6 chat robot