SyntaxError:JSON 中位置 49 处出现意外字符串
错误:
语法错误:JSON 中位置 49 处出现意外字符串
在 JSON.parse()
解析时 (C:\Users\goktu\OneDrive\Masaüstü\Flipkart\node_modules\body- 位于 C:\Users\goktu\OneDrive\Masaüstü\Flipkart\node_modules\body-parser
在 invokeCallback (C:\Users\goktu\OneDrive\Masaüstü\Flipkart\node_modu 完成时(C:\Users\goktu\OneDrive\Masaüstü\Flipkart\node_modules\raw-bo 在 IncomingMessage.onEnd (C:\Users\goktu\OneDrive\Masaüstü\Flipkart\no 在 IncomingMessage.emit (节点:事件:402:35)
在 endReadableNT(节点:内部/流/可读:1343:12)
在 processTicksAndRejections (节点:内部/进程/task_queues:83:21) 代码:
const express = require('express');
const env = require('dotenv');
const app = express();
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
// Routes
const userRoutes = require('./routes/user');
// Environment variable or you can say constants
env.config();
// MongoDB connection
// mongodb+srv://<username>:<password>@cluster0.kgufv.mongodb.net/myFirstDatabase?retryWrites=true&w=majority
mongoose.connect(
`mongodb+srv://${process.env.MONGO_DB_USER}:${process.env.MONGO_DB_PASSWORD}@cluster0.kgufv.mongodb.net/${process.env.MONGO_DB_DATABASE}?retryWrites=true&w=majority`,
{
useNewUrlParser: true,
useUnifiedTopology: true
}
).then(() => {
console.log('Database connected');
});
app.use(bodyParser());
app.use('api', userRoutes);
app.listen(process.env.PORT, () => {
console.log(`Server is running on port ${process.env.PORT}`);
});
我是编码新手,我认为 body-parser 库有问题,我不明白,你能帮助我吗?
Error:
SyntaxError: Unexpected string in JSON at position 49
at JSON.parse ()
at parse (C:\Users\goktu\OneDrive\Masaüstü\Flipkart\node_modules\body-
at C:\Users\goktu\OneDrive\Masaüstü\Flipkart\node_modules\body-parser
at invokeCallback (C:\Users\goktu\OneDrive\Masaüstü\Flipkart\node_modu
at done (C:\Users\goktu\OneDrive\Masaüstü\Flipkart\node_modules\raw-bo
at IncomingMessage.onEnd (C:\Users\goktu\OneDrive\Masaüstü\Flipkart\no
at IncomingMessage.emit (node:events:402:35)
at endReadableNT (node:internal/streams/readable:1343:12)
at processTicksAndRejections (node:internal/process/task_queues:83:21)
Code:
const express = require('express');
const env = require('dotenv');
const app = express();
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
// Routes
const userRoutes = require('./routes/user');
// Environment variable or you can say constants
env.config();
// MongoDB connection
// mongodb+srv://<username>:<password>@cluster0.kgufv.mongodb.net/myFirstDatabase?retryWrites=true&w=majority
mongoose.connect(
`mongodb+srv://${process.env.MONGO_DB_USER}:${process.env.MONGO_DB_PASSWORD}@cluster0.kgufv.mongodb.net/${process.env.MONGO_DB_DATABASE}?retryWrites=true&w=majority`,
{
useNewUrlParser: true,
useUnifiedTopology: true
}
).then(() => {
console.log('Database connected');
});
app.use(bodyParser());
app.use('api', userRoutes);
app.listen(process.env.PORT, () => {
console.log(`Server is running on port ${process.env.PORT}`);
});
I'm new to coding, I think there is a problem with the body-parser library, I don't understand, can you help me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这意味着您的请求包含无效的尝试,请使用此在线工具解析它以查找问题
在此处输入链接描述
另外避免自己处理主体解析器错误
我建议使用这个中间件
https://www.npmjs.com/package /express-body-parser-error-handler
Its mean your request contains an invalid try to parse it with this online tool to find the problem
enter link description here
Also to avoid handling body parser errors yourself
I recommend using this middleware
https://www.npmjs.com/package/express-body-parser-error-handler