简体   繁体   English

Express 应用程序的 Route.get() 需要回调函数,但出现 [object Undefined] 错误

[英]Route.get() for express app requires a callback function but got a [object Undefined] error

I have the following health.js file..我有以下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;

Which is being called in routes.jsroutes.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);
};

But when I try to run my app, I receive the following error..但是当我尝试运行我的应用程序时,我收到以下错误..

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

Why am I receiving this error?为什么我会收到此错误?

I'm not really clear what you're trying to do but I can explain why you're seeing that error.我不太清楚您要做什么,但我可以解释为什么您会看到该错误。

Here are the relevant bits of health.js :以下是health.js的相关health.js

var health = function(req, res, done) {};

health.prototype.endpoint = function(req, res, done) {
   ...
};

module.exports = health;

So you've got an empty function called health and then you've added something to it's prototype.所以你有一个名为health的空函数,然后你在它的原型中添加了一些东西。 Adding something to the prototype is a bit strange unless you're planning to create an instance of health , like this:除非您打算创建一个health实例,否则向原型添加一些东西有点奇怪,如下所示:

var myHealth = new health();
var endpoint = health.endpoint;

It seems unlikely that's what you had in mind.这似乎不太可能是你的想法。

In your routes.js you've got this:在你的routes.js你有这个:

health = require('./endpoints/health').endpoint,

The variable health will be undefined because there is no endpoint property for the exported function.变量health将是undefined因为导出的函数没有endpoint属性。 You could fix it by whacking in prototype but again I don't think it's really what you want:您可以通过敲击prototype来修复它,但我再次认为这不是您真正想要的:

health = require('./endpoints/health').prototype.endpoint,

If that empty health function needs to be maintained as the thing that gets exported then you could just get rid of the word prototype in health.js :如果这个空的health函数需要作为导出的东西来维护,那么你可以去掉health.js中的prototype这个词:

health.endpoint = function(req, res, done) {
    ...
};

However if you don't need that empty function and just want to export something called endpoint you could change health.js to something like this:但是,如果您不需要那个空函数并且只想导出称为endpoint东西,您可以将health.js更改为如下内容:

exports.endpoint = function(req, res, done) {
    ...
};
 health = require('./endpoints/health').endpoint,

尝试将健康作为一个模块,比如{health}

You have not posted the entire error.您还没有发布整个错误。 So that we can find the line number of error.这样我们就可以找到错误的行号。 But express usually throws this error if the callback provide to its route doesn't resolve to a function or is undefined.但是如果提供给其路由的回调没有解析为函数或未定义,则 express 通常会抛出此错误。 You can recheck if you have given correct path for each route.您可以重新检查是否为每条路线提供了正确的路径。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:Route.get() 需要一个回调函数,但在 app.js 中得到一个 [object Undefined] - Error: Route.get() requires a callback function but got a [object Undefined] at app.js 节点:Route.get() 需要一个回调函数,但得到了一个 [object Undefined] - Node: Route.get() requires a callback function but got a [object Undefined] 错误:Route.get()需要回调函数,但得到了一个[object Undefined] - Error: Route.get() requires callback functions but got a [object Undefined] 我想做基本的快速路由。 但它返回-&gt;错误:Route.get()需要回调函数,但得到了[object Undefined] - I want to do basic express routing. But it return -> Error: Route.get() requires a callback function but got a [object Undefined] 错误:Route.get()需要回调函数,但得到了一个[object Undefined] NODE.JS + SQL - Error: Route.get() requires a callback function but got a [object Undefined] NODE.JS + SQL 错误:Route.get() 需要回调 function 但在使用导入的 function 时得到 [object Undefined] - Error: Route.get() requires a callback function but got a [object Undefined] while using imported function 错误连接 MongoDB:错误:Route.get() 需要一个回调函数,但得到一个 [object Undefined] - Error connection MongoDB: Error: Route.get() requires a callback function but got a [object Undefined] 节点服务器错误:Route.get() 需要回调 function 但得到了 [object Undefined] - node server Error: Route.get() requires a callback function but got a [object Undefined] “错误:Route.get() 需要回调 function 但得到 [object Undefined]” 进行多次导出时 - “Error: Route.get() requires a callback function but got a [object Undefined]” when doing multiple exporting 错误:Route.get() 需要回调 function 但在 Node.js 中得到了一个 [object Undefined] - Error: Route.get() requires a callback function but got a [object Undefined] in Node.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM