0
我剛剛繼承了一個項目,有點隨意使用我不是很熟悉的工具建立。它使用Express,Bookshelf和Axios來訪問MySQL數據庫。 GET和PUT的快速路由似乎工作得很好,但POST和DELETE每個導致錯誤500.錯誤500上MySQL PUT和刪除通過Express,Axios
這裏是我使用的路由(我已經刪除了更多的GET和POST路由, ):
import express from 'express';
import Points from '../models/points';
let router = express.Router();
// GET all points associated with a specific user AND a specific session
// This works fine.
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});
})
});
// POST a single point to the database.
// This works fine.
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}));
});
// PUT (update) an existing point
// Doesn't work right now (500)
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'});
};
});
// DELETE a point by id
// Doesn't work right now (500)
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;
這裏是對應於上述的路由的終極版操作:
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)
}
}