Syntax check passes but uncaught SyntaxError: Unexpected identifier - javascript

Guys what else could be wrong when jshint, esprima all passes the code but chrome throws this error ? I am having a hard time figuring out.
P.S : Sorry I can't really share the code as I am not supposed to and I have to share the whole app code.

If you can't share the code, it will be hard for anyone to tell you what is wrong.
One thing you can do is simplify your code down to a concise test case, using the "divide and conquer" technique. Just start taking out big chunks of code, and keep repeating until one of the following happens:
The error goes away. This means the problem was in the last chunk of code you deleted. Put that code back in and take out only part of it and test again. Repeat this as needed.
Some other error crops up that wasn't there originally. Put back the last deleted chunk and either try deleting only a part of it, or deleting something else.
The error remains, but now there is so little code left that you will either see what the problem is, or else you won't have a problem posting it here for help. Once you have it down to a minimal test case like this, you can start renaming variables and functions so that it no longer reveals any company secrets.

Related

Trying to animate vexflow JavaScript

I am following these https://github.com/0xfe/vexflow/wiki/Animation-with-VexFlow-&-CSS instructions. I have copied the configuration verbatum, but error occurs: SVGConext is not defined. I have vexflow-debug.js included as a script. I'm not entirely sure if it defines SVGConent but it seems to. Anyway, I include svgcontent.js which seems sensible but it returns werror: Unspected token input.
Can you help?
I seem to have fixed it by going back and reentering everything. Sorry about that but I guess you get that a lot?
If it breaks again, I'll spend more time on my side.
Ian

Get code of specific line in Javascript file

When catching an error, I want to get with the stack trace the actual code of the line that triggered the error.
Given that I have the path to the file and the line number throwing the error, what's the best way to also get the actual code of that line?
Beyond the obvious of "looking it up in the file", there are a few things you can do to get it automatically. Checkout the Tracekit project on GitHub. For errors it captures, it will do an AJAX request for the script and find the relevant lines in the text.
Alternatively, if you're looking for a way to handle this automatically, you should consider a service like TrackJS that will capture all the relevant scripts and apply sourcemaps for you. I am one of the original developers and I've used it on many projects to fix bugs ridiculously fast :)

Are console errors acceptable in Javascript if they do not cause problems?

I'm a beginner at Javascript, and I wrote some code that works perfectly, but generates a console error from time to time.
It's a coin flip simulator, where the class of the coin needs to change depending on the outcome- so it searches for the opposite outcome class name and changes it to the current outcome class name (using SetAttribute). However, sometimes you get two of the same outcomes in a row, so searching for the opposite class name returns null, and then I get an error for trying to setAttribute of null.
I could write an If statement to avoid this, but I'm wondering if that's necessary. I want to learn best practices, so please, if there's a reason to avoid console errors, let me know!
Console errors such as an unhandled exception indicate a problem in your code. Sometimes the problem might turn out to be benign, but there is no way to know if its benign without studying the code. And, by the time you have studied the code to understand the problem, it is usually one or two lines of code to avoid or handle the problem in your code.
You should add this extra code to handle the problem because if you let these benign warnings build up, then pretty soon you won't be able to notice a new warning pop up that is actually a sign of serious trouble. Your code should be exception free precisely for this reason so when an unexpected issue shows up, you can clearly see it and both you and everyone else working on the project or testing the project knows that a warning like this is unexpected.
So, it is a good practice to clean up all warnings like this and keep your code clean and warning free.
Imagine you were driving your car and a warning light came on. You take it into the mechanic and the mechanic says: "oh, that's how they designed it - the light comes on from time to time". OK, now the warning is basically useless because you don't know when a warning is a sign of a real issue and when a warning is just some incomplete design that didn't clean things up well. It's the same with your code.

How to find the location of an error when line numbers cannot be used

I am finding it difficult to locate where an error occurs in javascript on a client I have no access to. Currently I trap the error with onerror and send the arguments to a log on the server.
Unfortunately the line number is no help because numerous javascript files get included, causing the line number to not correspond to anything I have access to.
So if I get something like "n is not defined", and n occurs many times in the function, I have no way to locate where it happened.
I have been trying to reference the code on the line throwing the error say "x=n * 5 + 4", then I could search for that code, but have had no luck referencing the actual code on a line from within javascript.
So how does one locate the line that threw the error in this situation?
client uses firefox only, if that matters.
I have no access to client
This is not one error I am stuck on, but working on how to track an error in this situation
Your best bet would be to use Firefox's debugger.
Open dev tools
Go to the debugger, select the .js file you want, and hit the little {} button in the bottom left (depending on version yours may be in a different location) -- this will prettify the JavaScript
Set breakpoints by clicking next to line numbers
From here on out you have to do this old-fashioned. Cast a breakpoint net around your trouble code, then keep narrowing down the lines until you find the occurrence that causes the error.
Of course, once you find the line it still won't be 1-to-1 with the original code, but hopefully the breakpoint exercise will at least reduce the scope of code/logic you have to dig through.
use your debugger to enable breaking on error. once you break, look at your locals for clues about your location. go up the stack and look at each frame.
you should be able to trace n up the stack and find out why it was null
the little {} that william suggested is also helpful

Uncaught TypeError without ability to find the reason

Every time I right-click anywhere in my Application, jQuery is going to throw an error at line 5095 saying:
Uncaught TypeError: undefined is not a function
To find the cause of this error I tried two ways without success:
1. Debug jQuery:
In the Dev-Tool's I debug jQuery at Line 5095. The Problem: When I move the cursor over the page, it will stop and break. I have NO possibility to get it to make a right click. Even if I press play in Chrome's Debug-Line which appears, it will instantly fire again, even if I won't move the cursor!
2. Searching the reason in my code:
Since this Error is thrown for a long time now I cannot tell where to search. I have multiple js-files. So I realized the error is being thrown when I release the mouse-button. So I searched ALL my code for ANY mouse(-up/down)-events without success (found several that wont fire if I set breakpoints)
Now I am out of ideas how to find the source of it. I thought debugging the line of jQuery where the error is thrown would give me access to the stack variables with possibly more information about its source. Any suggestions?
Please don't ask me to post code. Since I can't tell where the mistakes happen I might post thousands of lines of JavaScript. I need a logical way to find the Errorsource.
the error definitely isn't in jquery or jquery-ui files. it's most definitely in a plugin's code or your custom code.
think of where the error might be. just do a basic estimation. and put a console.log() before those lines and run it again. see if the error occurs before the console output. if it happens before, investigate the code that executes before this line and the javascript files that are included before this file. if it happens after do the opposite. You can narrow down by putting multiple console.log()s
you can look at events with
$._data(element, "events")
but you cant see where the bind is in your code
maybe someone knows

Categories

Resources