SyntaxError: Unexpected string in JSON at position 49

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?

Upvotes: 1

Views: 4247

Answers (1)

Naor Tedgi
Naor Tedgi

Reputation: 5707

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

Upvotes: 0

Related Questions