An unexpected project has landed in my lap, pieced together using tools that are unfamiliar to me. It utilizes Express, Bookshelf, and Axios to connect with a MySQL database. While the GET and PUT routes in Express seem to be functioning properly, issues arise with POST and DELETE requests resulting in error 500 responses.
Below are snippets of the routes I am working with (I've omitted additional routes that are operational):
import express from 'express';
import Points from '../models/points';
let router = express.Router();
// Successfully retrieves all points linked to a user within a specific session
router.get('/user/:user/session/:session', (req, res) => {
Points.query({
select: ['id', 'number', 'quadrant', 'level', 'title', 'category'],
where: {sessionId: req.params.session, userId: req.params.user}
}).fetchAll().then(point => {
res.json({point});
})
});
// Effectively adds a single point to the database
router.post('/', (req, res) => {
const {sessionId, userId, number, quadrant, level, title, category} = req.body;
Points.forge({
sessionId, userId, number, quadrant, level, title, category
}).save()
.then(user => res.json({success: true}))
.catch(err => res.status(500).json({error: err}));
});
// Struggles with updating an existing point (500 error)
router.put('/edit/:identifier', (req, res) => {
Points.update({
set: {title: req.params.title},
where: {id: req.params.identifier}
}), function (err, point) {
if (err) {
return res.send(err);
}
res.json({message: 'Updated'});
};
});
// Fails to delete a point by id (500 error)
router.delete('/delete/:identifier', (req, res) => {
Points.remove({
id: req.params.identifier
}), function (err, point) {
if (err) {
return res.send(err);
} else {
res.json({message: 'Deleted'});
}
};
});
export default router;
Additionally, here are the Redux actions associated with the aforementioned routes:
import axios from 'axios';
export function getPointsByUserAndSession(data) {
return dispatch => {
return axios.get('/api/points/user/'+data.user+'/session/'+data.session)
}
}
export function addPoint(data) {
return dispatch => {
return axios.post('/api/points', data)
}
}
export function editPointById(data) {
return dispatch => {
return axios.put('/api/points/edit/' + data.id)
}
}
export function deletePointById(identifier) {
return dispatch => {
return axios.delete('/api/points/delete/' + identifier)
}
}