-1

I'm stuck on the last bit of the fourth MDN tutorial where you're to run nodemon to check out your routes/controllers to see what you've done running them on a localhost. It says I have an error with routing and requires a callback function of an undefined object. I've googled and gone to the lesson's GitHub and gone through and redone code and read blocks of code myself and am still stuck. It appears to be an issue regarding the author GET section of my routes catalog. I've listed the code at the bottom.

I can't quite figure out what is wrong as I've copied and pasted the whole lesson and followed the instructions. I even thought that one of the issues was that I hadn't made it into a local repository and did that but it turned out it was actually a duplicate file in the parent folder of the same name so I had to cd express-locallibrary-tutorial and then was finally able to locate the files needed.

________@Bridgettes-MacBook-Air express-locallibrary-tutorial % DEBUG=express-locallibrary-tutorial:* npm run devstart

> express-locallibrary-tutorial@0.0.0 devstart /Users/_____/Desktop/express-locallibrary-tutorial/express-locallibrary-tutorial
> nodemon ./bin/www

[nodemon] 2.0.12
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node ./bin/www`
/Users/_______/Desktop/express-locallibrary-tutorial/express-locallibrary-tutorial/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.<computed> [as get] (/Users/_______/Desktop/express-locallibrary-tutorial/express-locallibrary-tutorial/node_modules/express/lib/router/route.js:202:15)
    at Function.proto.<computed> [as get] (/Users/_________/Desktop/express-locallibrary-tutorial/express-locallibrary-tutorial/node_modules/express/lib/router/index.js:510:19)
    at Object.<anonymous> (/Users/___________/Desktop/express-locallibrary-tutorial/express-locallibrary-tutorial/routes/catalog.js:56:8)
    at Module._compile (internal/modules/cjs/loader.js:1085:14)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
    at Module.load (internal/modules/cjs/loader.js:950:32)
    at Function.Module._load (internal/modules/cjs/loader.js:790:14)
    at Module.require (internal/modules/cjs/loader.js:974:19)
    at require (internal/modules/cjs/helpers.js:92:18)
    at Object.<anonymous> (/Users/__________/Desktop/express-locallibrary-tutorial/express-locallibrary-tutorial/app.js:13:21)
    at Module._compile (internal/modules/cjs/loader.js:1085:14)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
    at Module.load (internal/modules/cjs/loader.js:950:32)
    at Function.Module._load (internal/modules/cjs/loader.js:790:14)
    at Module.require (internal/modules/cjs/loader.js:974:19)
    at require (internal/modules/cjs/helpers.js:92:18)
[nodemon] app crashed - waiting for file changes before starting...

Line 56:8

// GET request to update Author.
router.get('/author/:id/update', author_controller.author_update_get);

Line 13:21

var catalogRouter = require('./routes/catalog');  //Import routes for "catalog" area of site
Bridgette
  • 9
  • 6
  • Please provide a [mre] **as text**; there's no call to .get anywhere in what you've shown, and screenshots aren't particularly useful. – jonrsharpe Aug 18 '21 at 17:31
  • okay, just a moment, let me do that asap :) – Bridgette Aug 18 '21 at 17:33
  • Error: Route.get() requires a callback function but got a [object Undefined] – Bridgette Aug 18 '21 at 17:35
  • is this what you mean? sorry I didn't mean to make it as long as I did I'm still learning how to ask questions correct. I believe its a callback issue but I can't seem to locate where – Bridgette Aug 18 '21 at 17:36
  • No, I mean [edit] the question to provide (just!) enough information for someone else to recreate and thereby hopefully solve the issue. I'd strongly recommend reading [ask], where this requirement is spelled out. – jonrsharpe Aug 18 '21 at 17:40
  • Note **minimal** - that's a lot of calls to get - and **reproducible** - where's the other bit? Did you check to see what the values of your various controllers _are_, if the properties you're trying to access are apparently undefined? – jonrsharpe Aug 18 '21 at 17:43
  • I've redone it. I tried to say early on that I'm so new I'm still trying to learn formatting, thank you for teaching me and redirecting me to the proper resources :) I'm still learning how to read the error messages and which code is needed to post for recreating, I'm very sorry for the inconvenience. Once my question is answered I am happy to delete this :) Thank you for editing it and my older question too, thankfully I was able to find an answer for that one and just posted it now. – Bridgette Aug 18 '21 at 18:06
  • You have the badge that says you took the tour so you should really know that getting an answer then deleting the question is **not** a good outcome. Either this is useful to other people and its answer(s) should stay available for them, or it's not and it should be closed and/or deleted. Note the [mre] page also links to resources on basic debugging, which you should also read. – jonrsharpe Aug 18 '21 at 18:10
  • I will, thank you so much! – Bridgette Aug 18 '21 at 18:12

1 Answers1

0

The problem is in your routes, in catalog router file you are not importing any file or not passing it to router.get() middleware. Suppose you exported author_list in catalog router. then your get method should be like this.

const {author_List} = require('../controllers/authController')
router.get('/authorList', author_List)
module.exports = router

if i could see your router file i can explaint it more easily.

Naeem
  • 173
  • 2
  • 10
  • alright just a moment! thank you so much!! – Bridgette Aug 18 '21 at 17:38
  • okay just uploaded the file to bottom of initial post :) – Bridgette Aug 18 '21 at 17:40
  • @Bridgette can you please highlight the line 56? or you can tell me here which router is in line 56 in your code? – Naeem Aug 18 '21 at 17:43
  • // GET request to update Author. router.get('/author/:id/update', author_controller.author_update_get); – Bridgette Aug 18 '21 at 17:51
  • ok can you please paste the code of author_update_get. did you export it properly? – Naeem Aug 18 '21 at 17:59
  • var catalogRouter = require('./routes/catalog'); //Import routes for "catalog" area of site I believe so, how can I check? – Bridgette Aug 18 '21 at 18:03
  • @Bridgette does `console.log(catalogRouter)` or using a debugger confirm or deny that belief? – jonrsharpe Aug 18 '21 at 18:11
  • @Bridgette I am asking about the controller code of author_update_get – Naeem Aug 18 '21 at 18:22
  • actually I've been able to fix everything! Thank you so much for your patience and help!! I still have other errors but am now able to figure out how to debug and troubleshoot using terminal :) but I don't quite understand what was wrong but I went back and redid the author controller file and it fixed the error – Bridgette Aug 18 '21 at 19:30
  • Router.get() requires a callback function, which means when we hit this route it will invoke the callback function, we passed into the router, but when we didn't pass a proper callback function it throws an error. You should read about callbacks in javascript and how they work. – Naeem Aug 19 '21 at 07:48