![](/img/trans.png)
[英]Error: Route.get() requires a callback function but got a [object Undefined] at app.js
[英]Route.get() for express app requires a callback function but got a [object Undefined] error
我有以下health.js
文件..
var health = function(req, res, done) {};
health.prototype.endpoint = function(req, res, done) {
let http_status = 200;
console.log("--- Sending health endpoint status of %s", http_status);
res.sendStatus(http_status);
done();
};
module.exports = health;
在routes.js
被調用
module.exports = function (app) {
let index = require('./endpoints/helloworld'),
health = require('./endpoints/health').endpoint,
metadata = require('./endpoints/metadata');
app.get('/', index.endpoint);
app.get('/health', health);
app.get('/metadata', metadata.endpoint);
};
但是當我嘗試運行我的應用程序時,我收到以下錯誤..
Attaching to myapp_app_1
app_1 | npm info it worked if it ends with ok
app_1 | npm info using npm@3.10.10
app_1 | npm info using node@v6.11.4
app_1 | npm info lifecycle my-app@1.0.0~prestart-local: my-app@1.0.0
app_1 | npm info lifecycle my-app@1.0.0~start-local: my-app@1.0.0
app_1 |
app_1 | > my-app@1.0.0 start-local /app
app_1 | > node ./src/index.js
app_1 |
app_1 | /app/node_modules/express/lib/router/route.js:202
app_1 | throw new Error(msg);
app_1 | ^
app_1 |
app_1 | Error: Route.get() requires a callback function but got a [object Undefined]
app_1 | at Route.(anonymous function) [as get] (/app/node_modules/express/lib/router/route.js:202:15)
app_1 | at EventEmitter.app.(anonymous function) [as get] (/app/node_modules/express/lib/application.js:482:19)
app_1 | at module.exports (/app/src/routes.js:10:9)
app_1 | at Object.<anonymous> (/app/src/index.js:12:20)
app_1 | at Module._compile (module.js:570:32)
app_1 | at Object.Module._extensions..js (module.js:579:10)
app_1 | at Module.load (module.js:487:32)
app_1 | at tryModuleLoad (module.js:446:12)
app_1 | at Function.Module._load (module.js:438:3)
app_1 | at Module.runMain (module.js:604:10)
app_1 |
app_1 | npm info lifecycle my-app@1.0.0~start-local: Failed to exec start-local script
app_1 | npm ERR! Linux 4.9.49-moby
app_1 | npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "run" "start-local"
app_1 | npm ERR! node v6.11.4
app_1 | npm ERR! npm v3.10.10
app_1 | npm ERR! code ELIFECYCLE
app_1 | npm ERR! my-app@1.0.0 start-local: `node ./src/index.js`
app_1 | npm ERR! Exit status 1
app_1 | npm ERR!
app_1 | npm ERR! Failed at the my-app@1.0.0 start-local script 'node ./src/index.js'.
app_1 | npm ERR! Make sure you have the latest version of node.js and npm installed.
app_1 | npm ERR! If you do, this is most likely a problem with the my-app package,
app_1 | npm ERR! not with npm itself.
app_1 | npm ERR! Tell the author that this fails on your system:
app_1 | npm ERR! node ./src/index.js
app_1 | npm ERR! You can get information on how to open an issue for this project with:
app_1 | npm ERR! npm bugs my-app
app_1 | npm ERR! Or if that isn't available, you can get their info via:
app_1 | npm ERR! npm owner ls my-app
app_1 | npm ERR! There is likely additional logging output above.
app_1 | npm ERR! Linux 4.9.49-moby
app_1 | npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "run" "start-local"
app_1 | npm ERR! node v6.11.4
app_1 | npm ERR! npm v3.10.10
app_1 | npm ERR! path npm-debug.log.896050243
app_1 | npm ERR! code EACCES
app_1 | npm ERR! errno -13
app_1 | npm ERR! syscall open
app_1 |
app_1 | npm ERR! Error: EACCES: permission denied, open 'npm-debug.log.896050243'
app_1 | npm ERR! at Error (native)
app_1 | npm ERR! { Error: EACCES: permission denied, open 'npm-debug.log.896050243'
app_1 | npm ERR! at Error (native)
app_1 | npm ERR! errno: -13,
app_1 | npm ERR! code: 'EACCES',
app_1 | npm ERR! syscall: 'open',
app_1 | npm ERR! path: 'npm-debug.log.896050243' }
app_1 | npm ERR!
app_1 | npm ERR! Please try running this command again as root/Administrator.
app_1 |
app_1 | npm ERR! Please include the following file with any support request:
app_1 | npm ERR! /app/npm-debug.log
為什么我會收到此錯誤?
我不太清楚您要做什么,但我可以解釋為什么您會看到該錯誤。
以下是health.js
的相關health.js
:
var health = function(req, res, done) {};
health.prototype.endpoint = function(req, res, done) {
...
};
module.exports = health;
所以你有一個名為health
的空函數,然后你在它的原型中添加了一些東西。 除非您打算創建一個health
實例,否則向原型添加一些東西有點奇怪,如下所示:
var myHealth = new health();
var endpoint = health.endpoint;
這似乎不太可能是你的想法。
在你的routes.js
你有這個:
health = require('./endpoints/health').endpoint,
變量health
將是undefined
因為導出的函數沒有endpoint
屬性。 您可以通過敲擊prototype
來修復它,但我再次認為這不是您真正想要的:
health = require('./endpoints/health').prototype.endpoint,
如果這個空的health
函數需要作為導出的東西來維護,那么你可以去掉health.js
中的prototype
這個詞:
health.endpoint = function(req, res, done) {
...
};
但是,如果您不需要那個空函數並且只想導出稱為endpoint
東西,您可以將health.js
更改為如下內容:
exports.endpoint = function(req, res, done) {
...
};
health = require('./endpoints/health').endpoint,
嘗試將健康作為一個模塊,比如{health}
您還沒有發布整個錯誤。 這樣我們就可以找到錯誤的行號。 但是如果提供給其路由的回調沒有解析為函數或未定義,則 express 通常會拋出此錯誤。 您可以重新檢查是否為每條路線提供了正確的路徑。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.