0

while running, my react app, the browser's console is showing the error like

app.js:40 Uncaught (in promise) SyntaxError: Unexpected end of input

kindly help me I'm stuck in this error for 1 month and I suffered a lot of online solutions but nothing helped.

This is my app.js file in reactjs app

fetch('http://localhost:8080/expenseitem', 
    {
        mode: 'no-cors',
        creadentials: 'same-origin',
        headers: {
            'Content-Type': 'application/json',
            "Access-Control-Allow-Origin": "*",
            "Access-Control-Allow-Credentials" : true
        }
    }).then(response => response.json()
      
    ).then(
        data => {
            console.log('success',  data);
            // setExpenses(data)
        }
    );

This is my Api file

const Joi = require('joi');
const express = require('express');
const res = require('express/lib/response');
const app = express();
const PORT = 8080;
app.use(express.json());


const expenseitem = [
    {
        id: 1, title: 'Books', amount: '300',
        date: new Date(2022, 5, 12)
    },
    {
        id: 2, title: 'Food', amount: '800',
        date: new Date(2022, 5, 12)
    },
    {
        id: 3, title: 'Shooping', amount: '1 lakh',
        date: new Date(2022, 5, 12)
    },
    {
        id: 4, title: 'Grocaries', amount: '90,000',
        date: new Date(2022, 5, 12)
    },
    {
        id: 5, title: 'Accesencories', amount: '30,000',
        date: new Date(2022, 5, 12)
    },
    { id: 6, title: 'laptop', amount: '90,000',
    date: new Date(2022, 5, 6)},
];


app.get('/', (req, res) => {
    res.send('hello shreya');
});

app.get('/expenseitem/', (req, res) => {
    res.send(expenseitem);
    res.set({
        "Content-Type": "application/json",
        "Access-Control-Allow-Origin": "*",
        "Access-Control-Allow-Credentials" : true 
    });
});

app.get('/expenseitem/:id', (req, res) => {
    const expense = expenseitem.find(c => c.id === parseInt(req.params.id));
    if (!expense) res.status(404).send('The expense with given is is not found');
    res.send(expense);
})
app.post('/expenseitem/', (req, res) => {

    const { error } = validateExpense(req.body);
    if (error) {
        res.status(400).send(result.error.details[0].message);
        return;
    }
    const expense = {
        id: expenseitem.length + 1,
        title: req.body.title
    };

    expenseitem.push(expense);
    res.send(expense);
});


app.put('/expenseitem/:id', (req, res) => {
    const expense = expenseitem.find(c => c.id === parseInt(req.params.id));
    if (!expense) res.status(404).send('The expense with given ID is not found')
    expense.title = req.body.title;
    res.send(expense);
});

function validateExpense(expense) {
    const schema = {
        title: Joi.string().min(3).required()
    };

}

const port = process.env.PORT || 8080;
app.listen(port, () => console.log(`its live on http://localhost:${PORT}`));

shreya suman
  • 71
  • 1
  • 5
  • check these https://stackoverflow.com/questions/43362431/uncaught-in-promise-syntaxerror-unexpected-end-of-json-input – Yaseen May 27 '22 at 08:59

1 Answers1

0

There's a related question here regarding no-cors: https://stackoverflow.com/a/45697474/983178

However, it looks like you're setting the response headers using res.set after you call res.send. They should be set before:

app.get('/expenseitem/', (req, res) => {
    res.set({
        "Content-Type": "application/json",
        "Access-Control-Allow-Origin": "*",
        "Access-Control-Allow-Credentials" : true 
    });
    res.send(expenseitem);
});
ourmaninamsterdam
  • 1,915
  • 15
  • 11