当前位置:网站首页>Syntaxerror: unexpected token r in JSON at position 0
Syntaxerror: unexpected token r in JSON at position 0
2022-04-23 20:30:00 【Different 213】
The error message is :SyntaxError: Unexpected token R in JSON at position 0
This is my error code :
// Query all article data
let articles = await Article.find().populate('author');
stay mongoose Use in populate Method to implement Collection Association , Cause template engine art-template Can't render , If used earlier JSON Data type conversion will report an error :SyntaxError: Unexpected token R in JSON at position 0
Solution one :populate After the method is called lean()
Method .
This is the modified code :
// Query all article data
let articles = await Article.find().populate('author').lean();
notes :
lean()
Method : Is to tell mongoose Returns a normal object , instead of mongoose Document object , I used it firstJSON.stringify()
This method converts the document object to a string , Remove all other attribute formats , Just leave the required data string !
Solution 2
// This document is : Blog foreground request processing file
const {
Article } = require('../../model/article');
// Import paging module
const pagination = require('mongoose-sex-page');
module.exports = async (req, res) => {
// Query data from database
let result = await pagination(Article).page(1).size(4).display(5).find().populate('author').exec();
let str = JSON.stringify(result);
let json = JSON.parse(str);
// res.send(json)
// Render the template and pass the data
res.render('home/default.art', {
json
});
}
Use
stringify()
Methods andparse()
Method , Convert objects to strings , Then use it againparse()
Method to JSON Format .
Be careful : When transferring data, the converted data is transferredjson
Variable .
版权声明
本文为[Different 213]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210550450972.html
边栏推荐
- Zdns was invited to attend the annual conference of Tencent cloud basic resources and share the 2020 domain name industry development report
- Devaxpress report replay: complete the drawing of conventional two-dimensional report + histogram + pie chart
- Building the tide, building the foundation and winning the future -- the successful holding of zdns Partner Conference
- Devexpress 14.1 installation record
- Go zero framework database avoidance Guide
- 2022DASCTF Apr X FATE 防疫挑战赛 CRYPTO easy_real
- Imitation Baidu map realizes the three buttons to switch the map mode by automatically shrinking the bottom
- Plato Farm元宇宙IEO上线四大,链上交易颇高
- Paper writing 19: the difference between conference papers and journal papers
- How does onlyoffice solve no route to host
猜你喜欢
Go zero framework database avoidance Guide
SQL Server Connectors By Thread Pool | DTSQLServerTP plugin instructions
Plato farm is one of the four largest online IEOS in metauniverse, and the transaction on the chain is quite high
Three. Based on ply format point cloud voxel model JS upload interface writing
Modeling based on catiav6
[stack and queue topics] - sliding window
A useless confession artifact
The construction and use of Fortress machine and springboard machine jumpserver are detailed in pictures and texts
Leetcode 74. Search two-dimensional matrix
2022dasctf APR x fat epidemic prevention challenge crypto easy_ real
随机推荐
Development of Matlab GUI bridge auxiliary Designer (functional introduction)
Solution: NPM err! code ELIFECYCLE npm ERR! errno 1
内网渗透之DOS命令
JDBC tool class jdbcfiledateutil uploads files and date format conversion, including the latest, simplest and easiest way to upload single files and multiple files
LeetCode 74、搜索二维矩阵
堡垒机、跳板机JumpServer的搭建,以及使用,图文详细
DNS cloud school rising posture! Three advanced uses of authoritative DNS
ArcGIS JS version military landmark drawing (dovetail arrow, pincer arrow, assembly area) fan and other custom graphics
Numpy sort search count set
Es keyword sorting error reason = fielddata is disabled on text fields by default Set fielddata = true on keyword in order
上海回应“面粉官网是非法网站”:疏于运维被“黑”,警方已立案
Building the tide, building the foundation and winning the future -- the successful holding of zdns Partner Conference
16MySQL之DCL 中 COMMIT和ROllBACK
The ODB model calculates the data and outputs it to excel
6-5 字符串 - 2. 字符串复制(赋值) (10 分)C语言标准函数库中包括 strcpy 函数,用于字符串复制(赋值)。作为练习,我们自己编写一个功能与之相同的函数。
After route link navigation, the sub page does not display the navigation style problem
論文寫作 19: 會議論文與期刊論文的區別
黑客的入侵方式你知道几种?
LeetCode 1337、矩阵中战斗力最弱的 K 行
【栈和队列专题】—— 滑动窗口