I keep getting Unexpected end of JSON input error when I test and it keeps crashing
import express from 'express';
import mongoose from 'mongoose';
import Messages from './dbMessages.js'
const app = express ()
const port = process.env.PORT || 9000;
app.use(express.json());
const connection_url = '...'
mongoose.connect(connection_url, {
useNewUrlParser: true,
useUnifiedTopology: true
})
app.get('/', (req, res)=> res.status(200).send('hello world'))
app.get('/messages/sync', (req, res) => {
Messages.find((err, data) => {
if(err) {
res.status(500).send(err)
} else {
res.status(200).send(data)
}
})
})
app.post('/messages/new', (req, res) => {
const dbMessage = req.body
Messages.create(dbMessage, (err, data) => {
if (err) {
res.status(500).send(err)
} else {
res.status(201).send(data)
}
})
})
app.listen(port, ()=>console.log(`Listening on localhost:${port}`))
with all these I don't know why I keep getting an error each time I test the get method using postman,
The error is likely coming from the client side. Do check the format of your data from the client side. For example, if you are using postman to test, make sure you do not add a comma at the end of your json data.