【问题标题】:Express multiple delete routes表达多条删除路线
【发布时间】:2016-06-28 16:25:35
【问题描述】:

我正在使用 MEAN 堆栈和 sequelize。我有两种情况想从表中删除记录:

1) 使用给定的 id 删除一条记录。

2) 删除满足某些条件的所有记录(共享一个 projectId 的所有记录)。为此,我尝试设置两条路线来处理每种情况。

客户端服务(案例一):

  this.deleteCampaign = function(id) {
    return $http.delete(campaignBaseUrl + id);
  };

客户端服务(案例2):

  this.deleteMultipleCampaigns = function(projectID) {
    return $http.delete(campaignBaseUrl+ 'foo/' + projectID);
  };

服务器端路由:

// I want case 1 to access this route (working)
router.delete('/:id', controller.destroy);
// I want case 2 to access this route (not working)
router.delete('/foo/:projectID', controller.destroyMultiple);

更新 服务器端控制器:

// Deletes multiple Campaign from the DB
// for a give project ID
export function destroyMultiple(req, res) {
  console.log('req.params:');
  console.log(req.params); // { projectID: '7' }
  Campaign.findAll({
    where: {
      projectId: req.params.projectID
    }
  })
    .then(handleEntityNotFound(res))
    .then(removeEntity(res))
    .catch(handleError(res));
}

// Deletes a single Campaign from the DB
export function destroy(req, res) {
  console.log('destroySingle:');
  Campaign.find({
    where: {
      _id: req.params.id
    }
  })
    .then(handleEntityNotFound(res))
    .then(removeEntity(res))
    .catch(handleError(res));
}

更新 2

服务器端控制器继续...

function removeEntity(res) {
  return function(entity) {
    if (entity) {
      return entity.destroy()
        .then(() => {
          res.status(204).end();
        });
    }
  };
}

当我从客户端运行 case2 时,我收到此错误: DELETE /api/campaigns/foo/7 500{"name":"SequelizeDatabaseError","message":"invalid input syntax for integer: \"foo\"","parent":

【问题讨论】:

  • 也许 'foo' 被传递到您的数据库查询而不是 58。destroyMultiple 如何解压参数?
  • 我也是这么想的,用这个来检查id参数:router.param('id', function (req, res, next, id) { console.log('id:'); console.log(id); next(); });id是正确传递的
  • 您确定为该请求调用了正确的处理程序(即destroyMultiple)吗?
  • 它应该是projectID 而不是id 所以也许它实际上是在调用 :id 路由。
  • @leetibbett 我不确定我是否理解,projectIdid 都是整数,它们如何确定路由?

标签: javascript express sequelize.js


【解决方案1】:

express.js 文档说明

中间件加载的顺序很重要:中间件函数 先加载的也会先执行。

这意味着在您当前的路由器配置中

// I want case 1 to access this route (working)
router.delete('/:id', controller.destroy);
// I want case 2 to access this route (not working)
router.delete('/foo/:projectID', controller.destroyMultiple);

第一个路由也匹配 url /api/campaigns/foo/7。第二条路由是否更好地匹配 url 并不重要。第一条路由捕获“foo/7”或“foo”作为参数:id。无论哪种方式,该值都不是整数。

我认为字段campaign.projectId 是一个整数并且boooooom:抛出Sequelize 错误。

可能的解决方案

首先放置更具体的路线。像这样:

router.delete('/foo/:projectID', controller.destroyMultiple);
router.delete('/:id', controller.destroy);

【讨论】:

    【解决方案2】:

    感谢@robertklep 帮助我找出问题所在。我认为它与路由有关,但问题在于服务器端控制器方法。我将其修改为:

    // Deletes multiple Campaign from the DB
    // for a give project ID
    export function destroyMultiple(req, res) {
      Campaign.destroy({
        where: {
          projectId: req.params.projectID
        }
      })
        .then(handleEntityNotFound(res))
        .then(() => {
          res.status(204).end();
        })
        .catch(handleError(res));
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-04-28
      • 1970-01-01
      • 1970-01-01
      • 2018-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多