【发布时间】: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