簡體   English   中英

Express 應用程序的 Route.get() 需要回調函數,但出現 [object Undefined] 錯誤

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM