添加DELETE操作会导致TypeError

时间:2018-09-04 07:40:53

标签: javascript node.js rest http-delete reserved-words

我创建了一个Node.js rest api。它遵循通用控制器,模型,架构结构,并使用我通过运行node index.js开始的restify服务器。

控制器由controllers/baseController.js组成,其中包含与我所有控制器类相关的一些操作。我的controllers/issues.js最初扩展了controllers/baseController.js,只有GET,POST和PUT操作。虽然不完整,但效果很好。他们当然没有阻止服务器启动。

然后我将以下删除操作添加到控制器中:

controller.addAction({
  'path': '/issues',
  'method': 'DELETE',
  'summary': 'Delete all issues from the list',
  'responseClass': 'Issues',
  'nickname': 'deleteIssues'
}, controller.delete);

它路由到:

delete(req, res, next) {
  let collectionContents = this.lib.db.model('Issues');
  collectionContents.remove({}, (err, result) => {
    if(err) return next(this.RESTError('InternalServerError', err));
    this.writeHAL(res, result);
  });
}

但是,当我尝试启动服务器时,出现此错误:

Debugger listening on ws://127.0.0.1:30333/71b18332-4ac6-49b8-b0ac-86517e9a2190
Debugger attached.
/Users/paulcarron/git/issue-tracker/controllers/baseController.js:19
      app[method.toLowerCase()](act['spec']['path'], act['action']);
                               ^

    at Issues.setUpActions (/Users/paulcarron/git/issue-tracker/controllers/baseController.js:15:18)
    at Object.setupRoutes (/Users/paulcarron/git/issue-tracker/lib/helpers.js:13:12)
    at Object.<anonymous> (/Users/paulcarron/git/issue-tracker/index.js:35:13)
    at Module._compile (module.js:632:14)
    at Object.Module._extensions..js (module.js:646:10)
    at Module.load (module.js:554:32)
    at tryModuleLoad (module.js:497:12)
    at Function.Module._load (module.js:489:3)
    at Function.Module.runMain (module.js:676:10)
    at startup (bootstrap_node.js:187:16)
    at bootstrap_node.js:608:3
TypeError: app[method.toLowerCase(...)] is not a function
baseController.js:19
    at actions.forEach.act (/Users/paulcarron/git/issue-tracker/controllers/baseController.js:19:32)
    at Array.forEach (<anonymous>)
    at Issues.setUpActions (/Users/paulcarron/git/issue-tracker/controllers/baseController.js:15:18)
    at Object.setupRoutes (/Users/paulcarron/git/issue-tracker/lib/helpers.js:13:12)
    at Object.<anonymous> (/Users/paulcarron/git/issue-tracker/index.js:35:13)
    at Module._compile (module.js:632:14)
    at Object.Module._extensions..js (module.js:646:10)
    at Module.load (module.js:554:32)
    at tryModuleLoad (module.js:497:12)
    at Function.Module._load (module.js:489:3)
    at Function.Module.runMain (module.js:676:10)
    at startup (bootstrap_node.js:187:16)
    at bootstrap_node.js:608:3
Waiting for the debugger to disconnect...

baseController.jshere

'method': 'DELETE'似乎有问题。但是,我不知道为什么。我认为可能与Swagger有关,因为我相信addGETaddPOST等功能已经存在,而且也许addDELETE不存在,但我检查了一下就可以了。有人可以在这里解释什么地方出问题吗?

其他信息。 我想我正在进步。我遇到问题的具体点是method.toLowerCase()。设置DELETE时,实际上是DELETE.toLowerCase(),但这是不允许的,因为delete是保留字(我认为)。看来正确吗?如果可以,我该如何解决?

0 个答案:

没有答案