[英]Error: Route.get() requires a callback function but got a [object Undefined] NODE.JS + SQL
I would like to separate the router.get () from queries from sql queries, but I had this error, when I declare the direct function as parameter of router.get () function it works. 我想将router.get()与sql查询的查询分开,但是当我将直接函数声明为router.get()函数的参数时,却出现了此错误,它可以工作。
User.js user.js的
const express = require('express');
const router = express.Router();
const database = require('../queries/userQueries');
router.get('/users',database.getAllUsers);
module.exports = router;
userQueries.js userQueries.js
function getAllUsers(req, res, next) {
res.locals.connection.query('SELECT * from usuarios',
function (error, results, fields) {
if (error){
res.send(error);
return;
}
res.send(results);
});
};
module.exports = getAllUsers;
Error: Route.get() requires a callback function but got a [object Undefined] at Route.(anonymous function) [as get] (/home/antonio/achaiAPI/node_modules/express/lib/router/route.js:202:15) at Function.proto.(anonymous function) [as get] (/home/antonio/achaiAPI/node_modules/express/lib/router/index.js:510:19) at Object.
错误:Route.get()需要回调函数,但在Route上获得了[object Undefined]。(匿名函数)[as get](/home/antonio/achaiAPI/node_modules/express/lib/router/route.js:202 :15),位于Object的Function.proto。(匿名函数)[/ get](/home/antonio/achaiAPI/node_modules/express/lib/router/index.js:510:19)。 (/home/antonio/achaiAPI/api/routes/user.js:11:8) at Module._compile (internal/modules/cjs/loader.js:678:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:689:10) at Module.load (internal/modules/cjs/loader.js:589:32) at tryModuleLoad (internal/modules/cjs/loader.js:528:12) at Function.Module._load (internal/modules/cjs/loader.js:520:3) at Module.require (internal/modules/cjs/loader.js:626:17) at require (internal/modules/cjs/helpers.js:20:18)
(/home/antonio/achaiAPI/api/routes/user.js:11:8)位于Object.Module._extensions..js(内部)的Module._compile(内部/modules/cjs/loader.js:678:30) /modules/cjs/loader.js:689:10)位于try.ModuleLoad(internal / modules / cjs / loader.js:528:12)具有Module.load(internal / modules / cjs / loader.js:589:32) Function.Module._load(internal / modules / cjs / loader.js:520:3)位于require。(internal / modules / cjs / loader.js:626:17)在require(internal / modules / cjs / helpers。 JS:20:18)
You're assigning the function getAllUsers
itself to the module.exports
, not to a property of module.exports
. 你分配功能
getAllUsers
本身的module.exports
,没有的属性 module.exports
。 So, when you import it with require
, that resolves to said function , not an object with that function as one of its properties. 因此,当您使用
require
导入它时,它将解析为所述function ,而不是将该函数作为其属性之一的对象。
Try assigning the imported object to a variable name representing the function instead: 尝试将导入的对象分配给代表该函数的变量名称:
const express = require('express');
const router = express.Router();
const getAllUsers = require('../queries/userQueries');
router.get('/users', getAllUsers);
module.exports = router;
You would use 你会用
const database = require('../queries/userQueries');
router.get('/users',database.getAllUsers);
when your userQueries.js
had assigned the function to a property of the exports
: 当您的
userQueries.js
将函数分配给exports
的属性时:
module.exports.getAllUsers = function getAllUsers( ...
getAllUsers is already a function,so you can Use directly. getAllUsers已经是一个函数,因此您可以直接使用。 change
router.get('/users',database.getAllUsers);
更改
router.get('/users',database.getAllUsers);
to router.get('/users',database);
到
router.get('/users',database);
. 。 you can yet return an object in
userQueries.js
file 您仍然可以在
userQueries.js
文件中返回对象
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.