【问题标题】:nodemon keeps crashing because of route.get() file errors由于 route.get() 文件错误,nodemon 不断崩溃
【发布时间】:2021-10-20 12:21:21
【问题描述】:

我被困在fourth MDN tutorial 的最后一点,您将在其中运行 nodemon 以检查您的路由/控制器,看看您在本地主机上运行它们做了什么。它说我有一个路由错误,需要一个未定义对象的回调函数。我已经用谷歌搜索并访问了课程的 GitHub,并自己浏览并重做代码并阅读了代码块,但仍然卡住了。这似乎是关于我的路线目录的作者 GET 部分的问题。我已经在底部列出了代码。

由于我复制并粘贴了整个课程并按照说明进行操作,因此我无法弄清楚哪里出了问题。我什至认为其中一个问题是我没有将它放入本地存储库并做到了,但事实证明它实际上是同名父文件夹中的重复文件,所以我不得不 cd express-locallibrary-教程,然后终于能够找到所需的文件。

________@Bridgettes-MacBook-Air express-locallibrary-tutorial % DEBUG=express-locallibrary-tutorial:* npm run devstart

> express-locallibrary-tutorial@0.0.0 devstart /Users/_____/Desktop/express-locallibrary-tutorial/express-locallibrary-tutorial
> nodemon ./bin/www

[nodemon] 2.0.12
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node ./bin/www`
/Users/_______/Desktop/express-locallibrary-tutorial/express-locallibrary-tutorial/node_modules/express/lib/router/route.js:202
        throw new Error(msg);
        ^

Error: Route.get() requires a callback function but got a [object Undefined]
    at Route.<computed> [as get] (/Users/_______/Desktop/express-locallibrary-tutorial/express-locallibrary-tutorial/node_modules/express/lib/router/route.js:202:15)
    at Function.proto.<computed> [as get] (/Users/_________/Desktop/express-locallibrary-tutorial/express-locallibrary-tutorial/node_modules/express/lib/router/index.js:510:19)
    at Object.<anonymous> (/Users/___________/Desktop/express-locallibrary-tutorial/express-locallibrary-tutorial/routes/catalog.js:56:8)
    at Module._compile (internal/modules/cjs/loader.js:1085:14)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
    at Module.load (internal/modules/cjs/loader.js:950:32)
    at Function.Module._load (internal/modules/cjs/loader.js:790:14)
    at Module.require (internal/modules/cjs/loader.js:974:19)
    at require (internal/modules/cjs/helpers.js:92:18)
    at Object.<anonymous> (/Users/__________/Desktop/express-locallibrary-tutorial/express-locallibrary-tutorial/app.js:13:21)
    at Module._compile (internal/modules/cjs/loader.js:1085:14)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
    at Module.load (internal/modules/cjs/loader.js:950:32)
    at Function.Module._load (internal/modules/cjs/loader.js:790:14)
    at Module.require (internal/modules/cjs/loader.js:974:19)
    at require (internal/modules/cjs/helpers.js:92:18)
[nodemon] app crashed - waiting for file changes before starting...

第 56:8 行

// GET request to update Author.
router.get('/author/:id/update', author_controller.author_update_get);

13:21 行

var catalogRouter = require('./routes/catalog');  //Import routes for "catalog" area of site

【问题讨论】:

  • 请提供minimal reproducible example 作为文本;在您显示的任何地方都没有调用 .get,屏幕截图也不是特别有用。
  • 好的,请稍等,让我尽快完成 :)
  • 错误:Route.get() 需要一个回调函数但得到了一个 [object Undefined]
  • 这是你的意思吗?抱歉,我不是故意的,只要我做到了,我还在学习如何正确提出问题。我认为这是一个回调问题,但我似乎无法定位
  • 不,我的意思是edit 提供(只是!)足够的信息供其他人重新创建,从而有望解决问题。我强烈建议您阅读How to Ask,其中详细说明了此要求。

标签: node.js routes controller nodemon


【解决方案1】:

问题在于您的路由,在目录路由器文件中,您没有导入任何文件或没有将其传递给 router.get() 中间件。 假设您在目录路由器中导出了 author_list。那么你的get方法应该是这样的。

const {author_List} = require('../controllers/authController')
router.get('/authorList', author_List)
module.exports = router

如果我能看到你的路由器文件,我会更容易解释。

【讨论】:

  • 好的,请稍等!非常感谢!!
  • 好的,刚刚将文件上传到初始帖子的底部:)
  • @Bridgette 你能突出显示第 56 行吗?或者您可以在此处告诉我代码中第 56 行中的哪个路由器?
  • // 获取更新作者的请求。 router.get('/author/:id/update', author_controller.author_update_get);
  • 好的,你能粘贴一下author_update_get的代码吗?是否正确导出?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-19
  • 2019-05-02
  • 1970-01-01
  • 2014-01-27
  • 2019-05-27
  • 1970-01-01
相关资源
最近更新 更多