错误500上MySQL PUT和删除通过Express,Axios

我刚刚继承了一个项目,有点随意使用我不是很熟悉的工具建立。它使用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)

}

}

回答:

启用用下面的代码CORS:

router.use((req, res, next) => { 

res.header('Access-Control-Allow-Origin', '*');

res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept, x-access-token');

res.header('Access-Control-Allow-Methods', 'GET, POST,OPTIONS, DELETE, PATCH, PUT');

next();

});

以上是 错误500上MySQL PUT和删除通过Express,Axios 的全部内容, 来源链接: utcz.com/qa/265748.html

回到顶部