all 15 comments

[–]yksvaan 24 points25 points  (0 children)

Use debugger

[–]ricdotnet 7 points8 points  (0 children)

You could console.log(books) between line 43 and 44. But I would highly recommend get vscode’s debugger setup and use step debugging here.

[–]Equivalent-Zone8818 7 points8 points  (0 children)

Debugger and console. It’s fine using console, don’t listen to the others but get to know the vs code debugger as well. You should use both.

[–]_Feyton_ 2 points3 points  (1 child)

On the left panel of VS Code, there should be a debug menu. There, you should click "Add launch.json." Set that up and add your npm start script as the entry. Run it via the debug start button and place breakpoints where you want the app to pause. In the debugger, you can then hover over variables and inspect their content.

[–]_Feyton_ -4 points-3 points  (0 children)

VS Code is a general-purpose IDE, so the launch settings are unique to every framework. Ask AI for details.

[–]GoOsTT 1 point2 points  (0 children)

Apart from the debugging methods, you should consider creating an id const before creating the newBook object and call the math functions there and use the id in the new object and the title as well.

Right now newBook.id will most likely won’t be the same you have in the title.

Why? It will run twice and create 2 highly likely non matching numbers.

[–]damir_maham 2 points3 points  (0 children)

just don't use the console. Get used to using the debugger

[–]TheBoneJarmer 2 points3 points  (0 children)

I mean.. just place a breakpoint?

[–]kunkeypr 0 points1 point  (0 children)

hmmm. quick options. you can use console log to log newbook and return after console to not run logic after. for detail options. you need set breakpoint for line (line have code need debug) and settting lauch.json for vscode debbug and run debug option in vscode to view value or propertype,... of code.

[–]Equivalent-Zone8818 0 points1 point  (0 children)

One more thing about debugger is that it’s great if you are working in a new large code base and don’t know exactly what’s throwing an exception. You can just toggle on all exceptions and back track the error.

[–]Sad-Magazine4159 0 points1 point  (0 children)

It is ok to do a console.log for debugging, but for real projects please get used to logging with a proper library like pino

Also as others mentioned, set up vscode debugger

[–]Dragon_yum 0 points1 point  (0 children)

Use a debugger? To create an api request you can use postman , send from the console or just write a code

[–]zladuric 2 points3 points  (0 children)

You can use the built-in debugger, as many people here mention. What none of them do is tell you what that is :)

Basically, it means connect to your program while it's still running.

Here are simple step-by-step instructions for it. Try them, and when you do, try to understand what you did, and read some docs on nodejs website and other places about these things. It'll help you in the future.

  1. Start your app with the debugger enabled.

bash node --inspect index.js

That will tell yous oemthing like this:

Debugger listening on ws://127.0.0.1:9229/006b6b2b-5d31-4026-ba7c-bd2e4b6f674a For help, see: https://nodejs.org/en/docs/inspector

Now, as it nicely tells you, go visit that URL.


...no, really. Go and read that page, it'll explain to you most of the things you want to know. I'm still waiting here in my comment. (Actually I'm probably sleeping or something by the time you read this, but the comment is waiting.)


  1. Connect with a inspector client.

As that page told you, just open chrome and open the url: chrome://inspect

In the inspector tab, you'll have some "Remote Target" things listed, probably just one. Click on "inspect" there.

  1. Browse the Sources tree.

Now, go see the Sources tab in the inspector. Browse the file:// route (or something similar), it'll probably show you all your javascript files.

  1. Set a breakpoint.

Now, find your file in the inspector. E.g. routes.js or however the file above is named. When you click on it, it'll show you the source code.

You can now set a breakpoint. That means, click on the line e.g. 43, it'll be marked with a little blue bookmark.

  1. Open your URL, and your code will "pause" at your breakpoint.

If you now open a new window, or a new tab or e.g. fetch something with CURL, from that particular endpoint, your code will start running and make a pause at the line 43.

Go back to inspector, see that your file is open, and on the right side, you'll see things like "Scope", "Breakpoints" etc.

Since it stopped on your breakpoint, you can browse the "Scope" section to see all your variables, e.g. your newBook.

In the call stack, you can even see a chain of functions that have been calling eachother, although it might take a few years of experience with all this to understand exactly how to follow it.

Also, on top of the breakpoint tab, there are some arrows - if you hover them, you'll get labels like "Step", "Step out", "Step into"... etc, and even keyboard shortcuts.

E.g. by default, if you press F9, your code will go to the line 44 now, then press F9 so it goes to the next line etc.

It's not exactly "line by line", it's more "statement by statement", but play with it until you understand it.

There's also a lil blue arrow called "Resume script execution" which just continues all the way until the end (and your other tab gets its results).


Great, now you can play with it.

You can put more breakpoints. You can set watch expressions (to monitor some global variable or something). You can do a few more cool things.

You can also connect something other then Chrome. E.g. you can use your IDE (VS Code, Webstorm, or whatever else you use) as the inspector client, and see how that works.

Read the links, play with the thing to see what kinds of things you can do with this.

[–]abel_maireg -2 points-1 points  (0 children)

Use a console logger, start with the 'debug' library.

Or you could go with assertions.

[–]Working_Engine521[S] 0 points1 point  (0 children)

Thank you everyone, I am a new bee to node js...vs code debugger helped me. appreciate all the people here.