当前位置:网站首页>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
边栏推荐
- [note] glide process and source code analysis
- Pyqt5 sensitive word detection tool production, operator's Gospel
- Report on the construction and development mode and investment mode of sponge cities in China 2022-2028
- Errors taken 1 Position1 argument but 2 were given in Mockingbird
- Solve the problem that the kaggle account registration does not display the verification code
- Sort merge sort
- Enter MySQL in docker container by command under Linux
- Live app source code, jump to links outside the station or jump to pages inside the platform
- Gossip about redis source code 83
- Pandaoxi's video
猜你喜欢
2022 t elevator repair registration examination and the latest analysis of T elevator repair
Yyds dry goods inventory [practical] simply encapsulate JS cycle with FP idea~
I wrote a chat software with timeout connect function
Recursive least square adjustment
Kubedl hostnetwork: accelerating the efficiency of distributed training communication
Tencent interview: can you pour water?
Fluent learning (4) listview
Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
The interviewer's biggest lie to deceive you, bypassing three years of less struggle
JDBC Technology
随机推荐
D23:multiple of 3 or 5 (multiple of 3 or 5, translation + solution)
Exclusive download! Alibaba cloud native brings 10 + technical experts to bring "new possibilities of cloud native and cloud future"
[note] IPC traditional interprocess communication and binder interprocess communication principle
Pyqt5 sensitive word detection tool production, operator's Gospel
Learning methods of zynq
[15th issue] Tencent PCG background development internship I, II and III (OC)
Research Report on the scale prediction of China's municipal engineering industry and the prospect of the 14th five year plan 2022-2028
ADB related commands
2022 chemical automation control instrument examination content and chemical automation control instrument simulation examination
2022 t elevator repair registration examination and the latest analysis of T elevator repair
Gossip about redis source code 81
Xiangong intelligent obtained hundreds of millions of yuan of b-round financing to accelerate the process of building non-standard solutions with standardized products
Schematic diagram of crystal oscillator clock and PCB Design Guide
Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
Hcip day 14 notes
Hcip day 12 notes
Introducing Software Testing
在恒泰证券开户怎么样?安全吗?
Errors taken 1 Position1 argument but 2 were given in Mockingbird
Go error collection | talk about the difference between the value type and pointer type of the method receiver