node-express-realworld-example-app icon indicating copy to clipboard operation
node-express-realworld-example-app copied to clipboard

Route.get() requires callback functions but got a [object Undefined]

Open coommark opened this issue 6 years ago • 44 comments

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 the router.get('/user' and router.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:

C:\production apps\backend\node_modules\express\lib\router\route.js:202
        throw new Error(msg);
        ^
Error: Route.get() requires callback functions but got a [object Undefined]
    at Route.(anonymous function) [as get] (C:\production apps\backend\node_modules\express\lib\router\route.js:202:15)
    at Function.proto.(anonymous function) [as get] (C:\production apps\backend\node_modules\express\lib\router\index.js:510:19)
    at Object.<anonymous> (C:\production apps\backend\routes\api\users.js:9:8)
    at Module._compile (module.js:569:30)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:503:32)
    at tryModuleLoad (module.js:466:12)
    at Function.Module._load (module.js:458:3)
    at Module.require (module.js:513:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (C:\production apps\backend\routes\api\index.js:3:17)
    at Module._compile (module.js:569:30)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:503:32)
    at tryModuleLoad (module.js:466:12)
    at Function.Module._load (module.js:458:3)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] start: `node ./app.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\BenKay\AppData\Roaming\npm-cache\_logs\2017-09-07T13_43_52_191Z-debug.log

coommark avatar Sep 07 '17 13:09 coommark

I've got the same exact issue. Did you ever figure this out?

mcblum avatar Oct 07 '17 16:10 mcblum

I´ve got the same problem

ghost avatar Nov 23 '17 12:11 ghost

check this!

https://stackoverflow.com/questions/36558909/route-get-requires-callback-functions-but-got-a-object-undefined

angelbrunn avatar Nov 26 '17 18:11 angelbrunn

i have some problem like that :(

DaSilvaJr avatar Dec 01 '17 07:12 DaSilvaJr

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 avatar Feb 08 '18 16:02 jessicabyrne

@jessicabyrne can you clarify what call that you removed? Thanks.

surferwat avatar Mar 24 '18 21:03 surferwat

I was missing module.exports = auth at the end of routes/api/users.js

tonilaukka avatar Jun 10 '18 12:06 tonilaukka

anyone please provide the solution

Anindo94 avatar Jul 11 '18 08:07 Anindo94

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'

eawww avatar Jul 13 '18 21:07 eawww

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.

Fathma avatar Sep 10 '18 10:09 Fathma

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.

SampritiKakoty avatar Sep 16 '18 12:09 SampritiKakoty

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.

MRJordanGracia avatar Dec 28 '18 17:12 MRJordanGracia

check this link out https://stackoverflow.com/questions/34853675/error-post-requires-callback-functions-but-got-a-object-undefined-not-work

shekankode avatar Jan 24 '19 07:01 shekankode

@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..

hakoemmy avatar Jul 12 '19 15:07 hakoemmy

in my case , i write a wrong function name , it happened

dh360 avatar Nov 17 '19 14:11 dh360

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

proxybee avatar Dec 03 '19 13:12 proxybee

check your syntax in your controller, i had that problem for hrs getting pissed off. turns out it was just a syntax error " }"

thurnye avatar Apr 11 '20 03:04 thurnye

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

Jood80 avatar Jun 16 '20 21:06 Jood80

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}

md1116 avatar Jun 16 '20 23:06 md1116

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

zarateganso10 avatar Jun 30 '20 20:06 zarateganso10

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.

ya332 avatar Jul 21 '20 05:07 ya332

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

ypedroo avatar Sep 24 '20 23:09 ypedroo

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

lucaslp16 avatar Jan 06 '21 05:01 lucaslp16

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

kamaleshsivaraj avatar Mar 02 '21 06:03 kamaleshsivaraj

@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..

Thank you for pointing that out. I was doing the same.

hiranyagarbh avatar Mar 05 '21 11:03 hiranyagarbh

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...

yysach avatar Mar 17 '21 14:03 yysach

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

`

this works I have tried it in my code. Thanks a lot, but do you an idea why?

Gichohi-Simon avatar Apr 29 '21 11:04 Gichohi-Simon

@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..

Made my day too!!

malkashlomowithz avatar Jul 12 '21 13:07 malkashlomowithz

just check if you are importig your functions properly on the routes and check the spellings

Rajveerbi avatar Jul 21 '21 06:07 Rajveerbi

in my case i accidentally put () for a middleware that did not accept params lol

nurulhiidayah avatar Jul 31 '21 10:07 nurulhiidayah