当前位置:网站首页>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
边栏推荐
- Mysql database and table building: the difference between utf8 and utf8mb4
- Es keyword sorting error reason = fielddata is disabled on text fields by default Set fielddata = true on keyword in order
- Identification of bolt points in aerial photography based on perception
- 上海回应“面粉官网是非法网站”:疏于运维被“黑”,警方已立案
- 【PTA】整除光棍
- 論文寫作 19: 會議論文與期刊論文的區別
- 论文写作 19: 会议论文与期刊论文的区别
- The ODB model calculates the data and outputs it to excel
- Tencent Qiu Dongyang: techniques and ways of accelerating deep model reasoning
- Es index (document name) fuzzy query method (database name fuzzy query method)
猜你喜欢
BMP JPEG 图片转换为矢量图像 ContourTrace
Building the tide, building the foundation and winning the future -- the successful holding of zdns Partner Conference
Customize timeline component styles
Monte Carlo py solves the area problem! (save pupils Series)
Commit and rollback in DCL of 16 MySQL
The construction and use of Fortress machine and springboard machine jumpserver are detailed in pictures and texts
LeetCode 542、01 矩阵
Sqoop imports tinyint type fields to boolean type
Error reported by Azkaban: Azkaban jobExecutor. utils. process. ProcessFailureException: Process exited with code 127
DNS cloud school rising posture! Three advanced uses of authoritative DNS
随机推荐
Why does ES6 need to introduce map when JS already has object type
[PTA] get rid of singles
Customize timeline component styles
[PTA] l1-006 continuity factor
6-5 字符串 - 2. 字符串复制(赋值) (10 分)C语言标准函数库中包括 strcpy 函数,用于字符串复制(赋值)。作为练习,我们自己编写一个功能与之相同的函数。
XXXI` Prototype ` displays prototype properties and`__ proto__` Implicit prototype properties
ArcGIS js api 4. X submergence analysis and water submergence analysis
Commit and rollback in DCL of 16 MySQL
Plato Farm元宇宙IEO上线四大,链上交易颇高
上海回应“面粉官网是非法网站”:疏于运维被“黑”,警方已立案
Actual measurement of automatic ticket grabbing script of barley network based on selenium (the first part of the new year)
論文寫作 19: 會議論文與期刊論文的區別
On BIM data redundancy theory
LeetCode 1351、统计有序矩阵中的负数
Scripy tutorial - (2) write a simple crawler
Experience of mathematical modeling in 18 year research competition
一. js的深拷贝和浅拷贝
Click an EL checkbox to select all questions
Tensorflow 2 basic operation dictionary
Building the tide, building the foundation and winning the future -- the successful holding of zdns Partner Conference