Typeerror: Cannot Read Property 'shdesc' of Undefined
JavaScript Errors and How to Fix Them
JavaScript tin be a nightmare to debug: Some errors information technology gives tin can be very difficult to understand at get-go, and the line numbers given aren't always helpful either. Wouldn't it exist useful to accept a listing where yous could look to find out what they hateful and how to set up them? Here you lot become!
Beneath is a list of the strange errors in JavaScript. Different browsers tin give y'all dissimilar messages for the same mistake, so there are several unlike examples where applicable.
How to read errors?
Earlier the list, allow'southward speedily expect at the structure of an error message. Understanding the structure helps understand the errors, and you'll accept less trouble if you come across whatsoever errors non listed hither.
A typical error from Chrome looks similar this:
Uncaught TypeError: undefined is not a office
The structure of the error is every bit follows:
- Uncaught TypeError: This role of the message is usually not very useful. Uncaught means the error was non caught in a
catchstatement, andTypeErroris the error's name. - undefined is not a role: This is the message part. With fault letters, you have to read them very literally. For case in this instance information technology literally means that the code attempted to use
undefinedlike information technology was a function.
Other webkit-based browsers, like Safari, give errors in a similar format to Chrome. Errors from Firefox are similar, simply exercise non always include the starting time part, and recent versions of Internet Explorer also give simpler errors than Chrome – only in this instance, simpler does not always mean better.
At present onto the actual errors.
Uncaught TypeError: undefined is not a function
Related errors: number is not a office, object is not a function, cord is not a function, Unhandled Error: 'foo' is not a function, Part Expected
Occurs when attempting to call a value like a function, where the value is not a office. For instance:
var foo = undefined; foo();
This error typically occurs if you are trying to telephone call a function in an object, merely you typed the name incorrect.
var ten = document.getElementByID('foo'); Since object backdrop that don't exist are undefined past default, the to a higher place would result in this error.
The other variations such as "number is non a office" occur when attempting to telephone call a number like it was a office.
How to ready this error: Ensure the role name is correct. With this error, the line number volition usually point at the correct location.
Uncaught ReferenceError: Invalid left-mitt side in assignment
Related errors: Uncaught exception: ReferenceError: Cannot assign to 'functionCall()', Uncaught exception: ReferenceError: Cannot assign to 'this'
Caused by attempting to assign a value to something that cannot exist assigned to.
The most common example of this error is with if-clauses:
if(doSomething() = 'somevalue')
In this example, the programmer accidentally used a single equals instead of two. The message "left-hand side in assignment" is referring to the part on the left side of the equals sign, so like you can see in the to a higher place example, the left-manus side contains something you can't assign to, leading to the error.
How to fix this mistake: Make sure you're non attempting to assign values to function results or to the this keyword.
Uncaught TypeError: Converting circular structure to JSON
Related errors: Uncaught exception: TypeError: JSON.stringify: Not an acyclic Object, TypeError: cyclic object value, Circular reference in value argument not supported
Always acquired by a circular reference in an object, which is and then passed into JSON.stringify.
var a = { }; var b = { a: a }; a.b = b; JSON.stringify(a); Because both a and b in the above example accept a reference to each other, the resulting object cannot exist converted into JSON.
How to fix this error: Remove circular references like in the example from any objects yous want to convert into JSON.
Unexpected token ;
Related errors: Expected ), missing ) after statement listing
The JavaScript interpreter expected something, but it wasn't in that location. Typically caused by mismatched parentheses or brackets.
The token in this mistake tin vary – it might say "Unexpected token ]" or "Expected {" etc.
How to set up this error: Sometimes the line number with this error doesn't point to the correct place, making it difficult to fix.
- An error with [ ] { } ( ) is ordinarily caused by a mismatching pair. Check that all your parentheses and brackets have a matching pair. In this case, line number will oft point to something else than the problem character
- Unexpected / is related to regular expressions. The line number for this will usually be correct.
- Unexpected ; is unremarkably caused by having a ; within an object or array literal, or within the argument list of a function call. The line number volition ordinarily be correct for this case as well
Uncaught SyntaxError: Unexpected token ILLEGAL
Related errors: Unterminated Cord Literal, Invalid Line Terminator
A string literal is missing the endmost quote.
How to fix this mistake: Ensure all strings have the correct closing quote.
Uncaught TypeError: Cannot read property 'foo' of naught, Uncaught TypeError: Cannot read property 'foo' of undefined
Related errors: TypeError: someVal is null, Unable to get property 'foo' of undefined or zippo reference
Attempting to read null or undefined every bit if it was an object. For example:
var someVal = nada; console.log(someVal.foo);
How to fix this mistake: Ordinarily caused by typos. Check that the variables used near the line number pointed past the error are correctly named.
Uncaught TypeError: Cannot set property 'foo' of cipher, Uncaught TypeError: Cannot prepare property 'foo' of undefined
Related errors: TypeError: someVal is undefined, Unable to set property 'foo' of undefined or zero reference
Attempting to write null or undefined as if it was an object. For example:
var someVal = nil; someVal.foo = 1;
How to fix this error: This likewise is usually caused by typos. Cheque the variable names near the line the error points to.
Uncaught RangeError: Maximum call stack size exceeded
Related errors: Uncaught exception: RangeError: Maximum recursion depth exceeded, too much recursion, Stack overflow
Usually caused by a issues in program logic, causing infinite recursive function calls.
How to gear up this error: Bank check recursive functions for bugs that could cause them to continue recursing forever.
Uncaught URIError: URI malformed
Related errors: URIError: malformed URI sequence
Caused by an invalid decodeURIComponent call.
How to fix this error: Check that the decodeURIComponent phone call at the error'due south line number gets right input.
XMLHttpRequest cannot load http://some/url/. No 'Access-Control-Allow-Origin' header is nowadays on the requested resource
Related errors: Cross-Origin Asking Blocked: The Aforementioned Origin Policy disallows reading the remote resource at http://some/url/
This error is always caused by the usage of XMLHttpRequest.
How to fix this error: Ensure the request URL is correct and information technology respects the same-origin policy. A good manner to find the offending code is to look at the URL in the mistake message and find it from your code.
InvalidStateError: An attempt was made to use an object that is not, or is no longer, usable
Related errors: InvalidStateError, DOMException code xi
Means the code called a function that you should not call at the current state. Occurs usually with XMLHttpRequest, when attempting to phone call functions on it before it'south ready.
var xhr = new XMLHttpRequest(); xhr.setRequestHeader('Some-Header', 'val'); In this case, y'all would get the error because the setRequestHeader office can just be called after calling xhr.open.
How to fix this error: Look at the code on the line pointed past the error and make sure information technology runs at the right time, or add any necessary calls earlier it (such as xhr.open)
Conclusion
JavaScript has some of the most unhelpful errors I've seen, with the exception of the notorious Expected T_PAAMAYIM_NEKUDOTAYIM in PHP. With more familiarity the errors start to make more sense. Modern browsers also assistance, every bit they no longer give the completely useless errors they used to.
What's the most disruptive error you lot've seen? Share the frustration in the comments!
Want to learn more most these errors and how to foreclose them? Find Problems in JavaScript Automatically with ESLint.
About Jani Hartikainen
Jani Hartikainen has spent over ten years building spider web applications. His clients include companies similar Nokia and hot super secret startups. When not programming or playing games, Jani writes near JavaScript and high quality code on his site.
codeutopia.netjhartikainenPosts
Source: https://davidwalsh.name/fix-javascript-errors
0 Response to "Typeerror: Cannot Read Property 'shdesc' of Undefined"
Enregistrer un commentaire