node-express-realworld-example-app
node-express-realworld-example-app copied to clipboard
Route.get() requires callback functions but got a [object Undefined]
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
I've got the same exact issue. Did you ever figure this out?
I´ve got the same problem
check this!
https://stackoverflow.com/questions/36558909/route-get-requires-callback-functions-but-got-a-object-undefined
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.
check this link out https://stackoverflow.com/questions/34853675/error-post-requires-callback-functions-but-got-a-object-undefined-not-work
@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