[英]Node JS : Route.get() requires a callback function but got a [object Undefined] While using ES6 Modules
Route File路由文件
scoreboardroute.js
import { scoreRouteController } from '../controllers/scoreboardcontroller';
const SCOREROUTE = app => {
app.route('/getAllScores').get(scoreRouteController.getAllScores);
};
export { SCOREROUTE };
Controller File控制器文件
scoreboardcontroller.js
import { scoreBoardModel } from '../model/scoreboardmodel';
class scoreRouteController {
getAllScores = (req, res) => {
scoreBoardModel.getAllScoresList((err, response) => {
if (err) {
res.send(err);
}
res.send(response);
});
};
}
export { scoreRouteController };
Model File:模型文件:
scoreboardmodel.js
import { db } from './db';
class scoreBoardModel {
getAllScoresList = callback => {
db.query('Select * from users', (err,response) => {
callback(err, response);
});
}
};
export { scoreBoardModel };
I was trying to use ES6 features like class and arrow functions inside my application.我试图在我的应用程序中使用 ES6 特性,如类和箭头函数。 While I'm trying to execute this code it hit the following error.
当我尝试执行此代码时,它遇到了以下错误。 I don't know what went wrong.
我不知道出了什么问题。 And I'm really new for node JS server.
我对节点 JS 服务器真的很陌生。 So, Can anyone please help me to get rid of this error.
所以,任何人都可以帮我摆脱这个错误。
Error :错误:
node_modules/express/lib/router/route.js:202 throw new Error(msg); 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.(anonymous function) [as get] (/node_modules/express/lib/router/route.js:202:15) at SCOREROUTE (/app/routes/scoreboardroute.js:4:32) at Object.错误:Route.get() 需要一个回调函数,但在 Route.(anonymous function) [as get] (/node_modules/express/lib/router/route.js:202:15) 在 SCOREROUTE ( /app/routes/scoreboardroute.js:4:32) 在对象。 (/server.js:26:1) at Module._compile (internal/modules/cjs/loader.js:689:30)
(/server.js:26:1) 在 Module._compile (internal/modules/cjs/loader.js:689:30)
I'm finding the answer.我正在寻找答案。
While importing the class I'm using like Object import.在导入我使用的类时,就像对象导入一样。 So, changed it like
所以,改变它像
import scoreRouteController from '../controllers/scoreboardcontroller';
And I'm not going to use so many instances for my application.我不会为我的应用程序使用这么多实例。 So, I assign static keyword for my every function inside my class.
因此,我为班级中的每个函数分配了 static 关键字。
static getAllScores = (req, res) => {.....
While exporting I was not exporting an Obj.导出时我没有导出Obj。 I changed into default class export.
我改为默认类导出。
export default scoreRouteController;
And finally, it works.最后,它起作用了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.