Open coommark opened 7 years ago
I've got the same exact issue. Did you ever figure this out?
I´ve got the same problem
i have some problem like that :(
I had the same problem. Turned out I had a call in my router.js that wasn't available anymore. When I removed it, the error went away.
@jessicabyrne can you clarify what call that you removed? Thanks.
I was missing module.exports = auth
at the end of routes/api/users.js
anyone please provide the solution
Check imports/requires and their respective exports for the use
function's second argument and make sure they match up. In my case, I made the dumb mistake of doing a named import on a component that didn't have named exports.
Example of doing it wrong:
export default ReallyNeatComponent
import { ReallyNeatComponent } from './ReallyNeatComponent'
In routes.js you might have called a function which doesn't exist in controller.js. make sure you have created all the functions that you are calling at the time of setting route.
I was getting same problem.
My authenticate file has method: exports.verifyUser = passport.authenticate('jwt', {session: false}); exports.verifyAdminUser = (req, res, next) => {
And my router file was:
uploadRouter.route('/') .get(authenticate.verifyUser, authenticate.verifyAdmin, (req, res, next) => { res.statusCode = 403; res.end('GET operation not supported on /imageUpload'); })
It was unable to find the function authenticate.verifyAdmin when i change it to authenticate.verifyAdminUser it worked.
So you might be missing a function which is required while setting the routes/middleware.
Hello, could you tell me how you implemented the function of verifyAdmin, I'm having de same problem and I'm not able to see how to solve it. Regards.
@eawww You made my day. I was mixing module.exporsts = auth
with export default router
, after I choose to use one and it works like charm..
in my case , i write a wrong function name , it happened
I had the same problem. Turned out I had a call in my router.js that wasn't available anymore. When I removed it, the error went away.
this worked for me too, thank you
check your syntax in your controller, i had that problem for hrs getting pissed off. turns out it was just a syntax error " }"
I got the same problem, the middleware was exported that way
exports.isAuthenticated = (req, res, next) => {....}
when I replaced it with
const isAuthenticated=(req, res, next)=>{.....}; module.exports = isAuthenticated
it works for me
anyone please provide the solution
I had the same issue and my mistake was using in express exports = {somefunctionName, anotherFunctionName}_ But looking at the above solution I did it like this and it works for me module.exports = {somefunctionName, anotherFunctionName}
i had the same issue, and my mistake was exporting the controller like a function and not a new object
module.exports = UserController wrong way
module.exports = new UserController() correct
I had the same issue and my problem was that I added .default in my require. file_a module.exports = dosomething file_b I changed this
const dosomething = require('file_a').default
to this
const dosomething = require('file_a')
and it fixed.
i had the same issue, and my mistake was exporting the controller like a function and not a new object
module.exports = UserController wrong way
module.exports = new UserController() correct
Reading this i cant belive how stupid i was by loosing 2 hours on this haha, thank you dude
Obtive o mesmo problema. Eu invoquei um middleware para a rota, porem havia colocado o module.exports do middleware dentro da indentação do código, mania de ficar dando enter para dar mais espaço na tela kkk
Fiquei feliz por descobrir o problema porem puto pq perdi uns 30min procurando kkk
Error: Route.get() requires a callback function but got a [object Undefined] at Route.
Instead of this:
app.get('/user/all',Controller.Create);
You try for:
app.get('/user/all', function(req, res){
Controller.Create
});
it worked for me! after a 15mins
@eawww You made my day. I was mixing
module.exporsts = auth
withexport default router
, after I choose to use one and it works like charm..
Thank you for pointing that out. I was doing the same.
anyone please provide the solution
I had the same issue and my mistake was using in express exports = {somefunctionName, anotherFunctionName}_ But looking at the above solution I did it like this and it works for me module.exports = {somefunctionName, anotherFunctionName}
thanks, I was doing the same mistake...
I got the same problem, the middleware was exported that way
exports.isAuthenticated = (req, res, next) => {....}
when I replaced it withconst isAuthenticated=(req, res, next)=>{.....}; module.exports = isAuthenticated
it works for me
`
this works I have tried it in my code. Thanks a lot, but do you an idea why?
@eawww You made my day. I was mixing
module.exporsts = auth
withexport default router
, after I choose to use one and it works like charm..
Made my day too!!
just check if you are importig your functions properly on the routes and check the spellings
in my case i accidentally put () for a middleware that did not accept params lol
check your syntax in your controller, i had that problem for hrs getting pissed off. turns out it was just a syntax error " }"
me 2 , I hate syntax errors :((((
C:\Users\dell\OneDrive\Desktop\liftech\BackendLiftech\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.
@imranfarhat you should post a snippet of your code that's complaining about too
Error: Route.post() requires a callback function but got a [object Object]
How to fix this?
the problem is pageRoute or your Route.js file. Check or write again
In my case I typod handler: as hander:
got the same error currently using Node.js v18.1.0 I have cloned a 9-month-old project from the git repo please help me to finger out it.
In the middleware I exported it as
exports.userController = (req,res,next)=>{.................}
I replaced it with const userController = (req,res,next) =>{...........} module.exports = userController
and it worked for me :),
also exports.userController = (req,res,next)=>{.................}
in route folder I imported it as const user = require('./auth)
and router.post('/user', user.userController)
- it worked both ways 👍
For more details have a look at stack over flow and export mdn docs
Hi, I got the same issue, Check if you correctely import your controller
Hi.. This issue actually bothered me for months so I will put my solution here to hopefully help someone.
So, I am building a backend using express and es6 class based controllers. I got this issue when I was creating the controller.
You have to make sure that u are exporting AND importing your controller correctly.
On my part I was exporting using -export default Controller- as an example. I realised that my problem happened when I was importing... you import by saying " import Controller from '../controllers/..' "
I found out that my problem was that after this step I missed something very important.. You are supposed to create an instance of the class so that u can have access to the various functions in your class.. so I had to do that by adding
const controller = new Controller()
After doing that, I had access to the functions in the class and the warning disappeared.
I hope this helps someone. Goodluck
The error is in the path: C:\production apps\backend\routes\api\users.js on line 9
Check if you have a function like the following
router.get("/test-route", callback function );
for example:
router.get("/test-route", (req, res) => { return res.status(200).json({ message: "I am a test action in my item controller" }) });
//file users.js-------------
const expres = require("express") const router = expres.Router();
const ItemController = require("../controllers/item"); router.get("/test-route", callback function imported from another module );
//file that exports the callback function example.js-------------
const test = (req, res) => { return res.status(200).json({ message: "I am a test action in my item controller" }); }
//Error made //module.export is misspelled //the import will not be carried out
module.export = { test }
//The correct way to write is
module.exports= { test }
My recommendation is that you check that each sentence is written correctly
The code is now correct
result
Error: Route.get() requires a callback function but got a [object Undefined] at Route. [as get] (node_modules) path
Instead of this:
app.get('/user/all',Controller.Create);
You try for:app.get('/user/all', function(req, res){ Controller.Create });
it worked for me! after a 15mins
This worked for me too. Does anyone know why the first way is incorrect? It seems ok to me
Always ensure the export name is the same as the import name
I encountered the same issue and resolved it by meticulously reviewing my import and export statements for every file and function. This turned out to be one of the causes leading to the problem.
I am attempting to recreate this application from scratch. So far I have users controller in place (without the articles, profiles and tags controllers.
However when I try to run the server, I get
Route.get() requires callback functions but got a [object Undefined]
error. If I comment out therouter.get('/user'
androuter.put('/user'
routes, everything works okay and I can register users without problem.I have been on this for hours. please does anyone know what the problem might be and how to fix it?
Here is complete error message: