Back end: e-commerce project

I am in Fullstack Path and currently working on back end e-commerce project. My project is based on other participant who has pubished his completed project. Furthermore, I do not just copy and paste for my project, but developed it to my own project. I have not finished it yet and I am perplexed where is the bug as I got the following error notification:
throw new Error(msg);

Error: requires a callback function but got a [object Undefined]
at Route. [as post] (C:\Users\nikol\OneDrive\Desktop\e-commerce\node_modules\express\lib\router\route.js:211:15)
at proto. [as post] (C:\Users\nikol\OneDrive\Desktop\e-commerce\node_modules\express\lib\router\index.js:521:19)
at Object. (C:\Users\nikol\OneDrive\Desktop\e-commerce\routes\index.js:13:20)
at Module._compile (node:internal/modules/cjs/loader:1254:14)
at Module._extensions…js (node:internal/modules/cjs/loader:1308:10)
at Module.load (node:internal/modules/cjs/loader:1117:32)
at Module._load (node:internal/modules/cjs/loader:958:12)
at Module.require (node:internal/modules/cjs/loader:1141:19)
at require (node:internal/modules/cjs/helpers:110:18)
at Object. (C:\Users\nikol\OneDrive\Desktop\e-commerce\server.js:12:70)

Node.js v18.16.0
I had checked the internet and according to their solution, I have tried to find the mistake , but to np avail. My project can be seen on: GitHub - BiruSkai/-independent-project-e-commerce. I would appreciate any of your assistance.


1 Like

The error hints at what you should be thinking about
Error: requires a callback function but got a [object Undefined]

and for the eagle-eyed it happens on line 13 at index.js (you don’t need to look at the source code, the error gives you this).

So that means when you call'/', registerUser);

the interpreter is expecting a callback function (as its second argument) but instead it gets an undefined object.

From here I leave it to you to figure out. You might say gee, i’m pretty sure registerUser is a callback function, then you have to consider what the state of it by that point.

  • did you import it correctly?
  • does it have some sort of async (any use of async/away, then() etc), in that case you can only call it when it is delivered and deal with that.

I highly highly higly highly recommend you use a debugger when working.

You can do something like

let x = 5;
x +=  2;

and then call your script and debug

node inspect your-script.js

For example

$ node inspect test.js
< Debugger listening on ws://
< For help, see:
< Debugger attached.
Break on start in test.js:1
> 1 var thisIsAVar = 5;
  2 debugger;
  3 const someVar = () => {
debug> n
break in test.js:2
  1 var thisIsAVar = 5;
> 2 debugger;
  3 const someVar = () => {
  4     basicThing();
debug> exec console.log(thisIsAVar);
< 5
1 Like

Thanks for the response. The error notification disappeared after adding curly braces on export statement.
module.exports = registerUser;
After solution:
module.exports = {registerUser}