Я работал над приложением среднего стека, и все было в порядке, пока это внезапно не начало появляться.
SyntaxError: Unexpected token } in JSON at position 23
at JSON.parse (<anonymous>)
at parse (C:\Users\Mushood Hanif\Documents\Work\Task-Manager\api\node_modules\body-parser\lib\types\json.js:89:19)
at C:\Users\Mushood Hanif\Documents\Work\Task-Manager\api\node_modules\body-parser\lib\read.js:121:18
at invokeCallback (C:\Users\Mushood Hanif\Documents\Work\Task-Manager\api\node_modules\raw-body\index.js:224:16)
at done (C:\Users\Mushood Hanif\Documents\Work\Task-Manager\api\node_modules\raw-body\index.js:213:7)
at IncomingMessage.onEnd (C:\Users\Mushood Hanif\Documents\Work\Task-Manager\api\node_modules\raw-body\index.js:273:7)
at IncomingMessage.emit (events.js:333:22)
at endReadableNT (_stream_readable.js:1220:12)
at processTicksAndRejections (internal/process/task_queues.js:84:21)
это код API:
const express = require('express');
const app = express();
const { mongoose } = require('./db/mongoose');
// Load in the mongoose Models
const { list, task } = require('./db/models');
// Load Middleware
app.use(express.json());
// CORS Headers Middleware
app.use(function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Methods", "GET, POST, HEAD, OPTIONS, PUT, PATCH, DELETE");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, x-access-token, x-refresh-token, _id");
res.header(
'Access-Control-Expose-Headers',
'x-access-token, x-refresh-token'
);
next();
});
// ROUTE HANDLERS
// LIST ROUTES
/**
* GET /lists
* Purpose: Get all Lists
*/
app.get('/lists', (req, res) => {
list.find({}).then((lists) => {
res.send(lists);
})
})
/**
* POST /lists
* Purpose: Create a List
*/
app.post('/lists', (req, res) => {
let title = req.body.title;
let newList = new list({
title
});
newList.save().then((listDoc) => {
res.send(listDoc);
})
});
/**
* PATCH /lists/:id
* Purpose: Update a List
*/
app.patch('/lists/:id', (req, res) => {
list.findOneAndUpdate({_id: req.params.id}, {
$set: req.body
}).then(() => {
res.sendStatus(200);
})
});
/**
* DELETE /lists/:id
* Purpose: Delete a List
*/
app.delete('/lists/:id', (req, res) => {
list.findByIdAndRemove({_id: req.params.id}).then((removedListDoc) => {
res.send(removedListDoc);
})
});
// TASK ROUTES
/**
* GET /lists/:listId/tasks
* Purpose: Get all Tasks in a Specific List
*/
app.get('/lists/:listId/tasks', (req, res) => {
task.find({
_listId: req.params.listId
}).then((tasks) => {
res.send(tasks);
})
});
/**
* GET /lists/:listId/tasks/:taskId
* Purpose: Get one Task from a Specific List
*/
app.get('/lists/:listId/tasks/:taskId', (req, res) => {
task.findOne({
_taskId: req.params.taskId,
_listId: req.params.listId
}).then((task) => {
res.send(task);
})
});
/**
* POST /lists/:listId/tasks
* Purpose: Create a New Task in a Specified List
*/
app.post('/lists/:listId/tasks', (req, res) => {
let newTask = new task({
title: req.body.title,
_listId: req.params.listId
});
newTask.save().then((newTaskDoc) => {
res.send(newTaskDoc);
})
});
/**
* PATCH /lists/:listId/tasks/:taskId
* Purpose: Update a Task from an Existing List
*/
app.patch('/lists/:listId/tasks/:taskId', (req, res) => {
task.findOneAndUpdate({
_id: req.params.taskId,
_listId: req.params.listId
}, {
$set: req.body
}).then(() => {
res.sendStatus(200);
})
});
/**
* DELETE /lists/:listId/tasks/:taskId
* Purpose: Delete a Task from an Existing List
*/
app.delete('/lists/:listId/tasks/:taskId', (req, res) => {
task.findOneAndRemove({
_id: req.params.taskId,
_listId: req.params.listId
}).then((removedTaskDoc) => {
res.sendStatus(removedTaskDoc);
})
});
app.listen(3000, () => {
console.log("Server Listening at Port 3000")
})
express включил в себя анализатор тела, и я использую это. Я чувствую, что это может быть причиной ошибки. Я не могу понять, откуда исходит ошибка. Я чувствую, что это синтаксическая ошибка где-то в моем node.js API, но я не могу точно определить это.