表示多个删除路由

时间:2016-06-28 16:25:35

标签: javascript express sequelize.js

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

1)使用给定的id删除单个记录。

2)删除满足某些条件的所有记录(共享projectId的所有记录)。为了实现这个目的,我试图建立两条路线来处理每个案例。

客户端服务(案例1):

func applicationWillFinishLaunching(_ notification: Notification) {
    NSAppleEventManager.shared().setEventHandler(self, andSelector: #selector(self.handleGetURL(event:reply:)), forEventClass: UInt32(kInternetEventClass), andEventID: UInt32(kAEGetURL) )
}

func handleGetURL(event: NSAppleEventDescriptor, reply:NSAppleEventDescriptor) {
    if let urlString = event.paramDescriptor(forKeyword: keyDirectObject)?.stringValue {
        print("got urlString \(urlString)")
    }
}

客户端服务(案例2):

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

服务器端路由:

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

更新2

服务器端控制器...

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

当我从客户端运行case2时,我收到此错误: function removeEntity(res) { return function(entity) { if (entity) { return entity.destroy() .then(() => { res.status(204).end(); }); } }; } DELETE /api/campaigns/foo/7 500

2 个答案:

答案 0 :(得分:0)

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。如果第二条路线更好地匹配网址则无关紧要。第一个路径捕获'foo / 7'或'foo'作为参数:id。无论哪种方式,该值都不是整数。

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

可能的解决方案

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

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

答案 1 :(得分:0)

感谢@robertklep帮助我确定问题。我认为这是与路由,但问题是在服务器端控制器方法。我将其修改为:

HAND = Enum('HAND', ['LH', 'RH'])


class HelicalGear(Gear):
    def __init__(self, hand):
        self.type_ = 'Helical'
        self.hand = hand

    @property
    def hand(self):
        return self._hand

    @hand.setter
    def hand(self, hand):
        if not hand:
            raise ValueError("Gear hand definition required")
        elif hand not in [e.name for e in HAND]:
            raise ValueError("Gear hand must be LH or RH")

        self._hand = hand