【问题标题】:Error 500 on MySQL PUT and DELETE via Express, Axios通过 Express、Axios 在 MySQL PUT 和 DELETE 上出现错误 500
【发布时间】:2017-12-27 14:37:48
【问题描述】:

我刚刚继承了一个使用我不太熟悉的工具随意构建的项目。它使用 Express、Bookshelf 和 A​​xios 来访问 MySQL 数据库。 GET 和 PUT 的 Express 路由似乎工作得很好,但 POST 和 DELETE 都会导致错误 500。

这是我正在使用的路由(我已经删除了更多可以正常工作的 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;

下面是与上述路由对应的 Redux 操作:

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)
    }
}

【问题讨论】:

    标签: javascript mysql express redux axios


    【解决方案1】:

    使用以下代码启用 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();
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-05-23
      • 2019-04-27
      • 2020-12-07
      • 1970-01-01
      • 2021-12-27
      • 2012-02-13
      • 2018-11-17
      相关资源
      最近更新 更多