Recently, I have been working on a small full-stack application with React as my front end and Node as my back end. In my server, I am using an endpoint with a get request to pull my data from the database, which is in sqlite. It was working properly until I started re-migrating (rollback/latest) the columns into my database. I could retrieve my seed data from Insomnia using the get request. Eventually, I added new columns and it started throwing this error below.
‘SyntaxError: Expected property name or ‘}’ in JSON at position 8
at JSON.parse ()’
So I rebuilt the entire project, tried it on similar projects, and still no dice. My migrations and seed files ran perfectly fine. In my server file, I have some middleware with server.use(express.json());
. I know this is one of the roots of my problem but I am not sure how to go about troubleshooting this. I have tried to reinstall packages and Node. Any suggestions/tips?
My server file code for reference:
const express = require('express');
const cors = require('cors');
const helmet = require('helmet');
// const testData = require('../testData');
const db = require('./dbConfig');
const server = express();
/*middleware*/
server.use(express.json());
server.use(cors());
server.use(helmet());
/*middleware*/
/*endpoints*/
server.get("https://stackoverflow.com/", (req,res) => (
res.send('Good luck lol')
));
server.get('/orderforms', async (req,res) => {
//GET all orderforms
// res.json(testData);
console.log("RECEIVED GET REQUEST");
try {
const orderforms = await db('orderforms');
res.status(200).json(orderforms);
} catch (err) {
res.status(500).json({ message: 'Internal Server Error.', error: err.message, stack: err.stack });
}
});
server.post('/orderforms', (req,res) => {
//Post an orderform
});
server.put('/orderforms/:id', (req,res) => {
//Put an orderform
});
server.delete('/orderforms/:id', (req,res) => {
//Delete an orderform
});
module.exports = server; //exports the server
I tried to remove the parser, which worked, but I need that to automatically parse the data. I had backtracked my steps and even rebuilt the entire project on the backend. When I tested the get request using a testData file it still threw the same exact error because of the express.json() middleware.
Where exactly are you seeing this error reported? Does it refer to any particular part of your code? If I had to guess, I’d say it’s coming from the
express.json()
middleware trying to parse a request body that says its JSON but is invalid. If that’s the case, the problem is client-side and has nothing to do with your server-side codeI received this error in my terminal before the get request even started. None of the log statements execute ( the RECEIVED REQUEST log). I have no client-side necessarily, insomnia is just using a localhost route to pull the data I have populated in my database on that localhost server. Are you saying the issue could be with insomnia itself?
Do you mean the terminal where you run / start your server? The error should include a stack trace, please edit your question to include it
I would change the middleware order to helmet, cors then json. Please also include specific details about how you’re making any requests