当前位置:网站首页>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
边栏推荐
- Parsing methods of JSON data in C - jar and jobobject: error reading jar from jsonreader Current JsonReader item
- LeetCode 542、01 矩阵
- Matlab analytic hierarchy process to quickly calculate the weight
- Commit and rollback in DCL of 16 MySQL
- 三十一. `prototype`显示原型属性和`__proto__`隐式原型属性
- Shanghai responded that "flour official website is an illegal website": neglect of operation and maintenance has been "hacked", and the police have filed a case
- Development of Matlab GUI bridge auxiliary Designer (functional introduction)
- 【栈和队列专题】—— 滑动窗口
- LeetCode 20、有效的括号
- Solution: NPM err! code ELIFECYCLE npm ERR! errno 1
猜你喜欢
Devexpress 14.1 installation record
上海回應“面粉官網是非法網站”:疏於運維被“黑”,警方已立案
Don't bother tensorflow learning notes (10-12) -- Constructing a simple neural network and its visualization
[PTA] l1-002 printing hourglass
[PTA] get rid of singles
Actual measurement of automatic ticket grabbing script of barley network based on selenium (the first part of the new year)
Leetcode 994, rotten orange
JDBC tool class jdbcfiledateutil uploads files and date format conversion, including the latest, simplest and easiest way to upload single files and multiple files
Zdns was invited to attend the annual conference of Tencent cloud basic resources and share the 2020 domain name industry development report
Handwritten Google's first generation distributed computing framework MapReduce
随机推荐
Recognition of high-speed road signs by Matlab using alexnet
A login and exit component based on token
[PTA] get rid of singles
Don't bother tensorflow learning notes (10-12) -- Constructing a simple neural network and its visualization
[PTA] l2-011 play with binary tree
16MySQL之DCL 中 COMMIT和ROllBACK
A useless confession artifact
[graph theory brush question-4] force deduction 778 Swimming in a rising pool
The market share of the financial industry exceeds 50%, and zdns has built a solid foundation for the financial technology network
【PTA】L1-006 连续因子
论文写作 19: 会议论文与期刊论文的区别
The problem of 1 pixel border on the mobile terminal
Analysis of the relationship between generalized Bim and CAD under the current background
Automatically fill in body temperature and win10 task plan
Leetcode 1351. Negative numbers in statistical ordered matrices
The second method of file upload in form form is implemented by fileitem class, servletfileupload class and diskfileitemfactory class.
selenium. common. exceptions. WebDriverException: Message: ‘chromedriver‘ executable needs to be in PAT
Click an EL checkbox to select all questions
16MySQL之DCL 中 COMMIT和ROllBACK
Installation and use of NVM