SyntaxError:JSON 中位置 49 处出现意外字符串

发布于 2025-01-10 12:38:51 字数 1663 浏览 0 评论 0原文

错误:

语法错误: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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

醉殇 2025-01-17 12:38:51

这意味着您的请求包含无效的尝试,请使用此在线工具解析它以查找问题

在此处输入链接描述

另外避免自己处理主体解析器错误
我建议使用这个中间件

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文