Lompat ke konten Lompat ke sidebar Lompat ke footer

App.js:115 Uncaught Typeerror: Cannot Read Property 'insertadjacenthtml' of Null

JavaScript Errors and How to Prepare Them

JavaScript can be a nightmare to debug: Some errors it gives tin can be very difficult to empathize at first, and the line numbers given aren't always helpful either. Wouldn't information technology be useful to have a list where you could expect to detect out what they mean and how to fix them? Hither you go!

Below is a list of the strange errors in JavaScript. Different browsers can give you different messages for the aforementioned fault, so there are several unlike examples where applicable.

How to read errors?

Before the list, let's speedily look at the structure of an error message. Understanding the structure helps understand the errors, and you'll take less problem if you come across any errors not listed here.

A typical error from Chrome looks like this:

Uncaught TypeError: undefined is not a role

The construction of the error is as follows:

  1. Uncaught TypeError: This part of the message is usually not very useful. Uncaught means the error was not caught in a catch statement, and TypeError is the fault'southward name.
  2. undefined is not a function: This is the message part. With fault letters, you take to read them very literally. For example in this instance it literally means that the code attempted to employ undefined like it was a function.

Other webkit-based browsers, like Safari, give errors in a similar format to Chrome. Errors from Firefox are similar, but exercise non always include the get-go part, and recent versions of Cyberspace Explorer also give simpler errors than Chrome – but in this case, simpler does not always mean better.

Now onto the actual errors.

Uncaught TypeError: undefined is not a function

Related errors: number is not a role, object is non a part, cord is not a function, Unhandled Error: 'foo' is not a function, Office Expected

Occurs when attempting to phone call a value like a function, where the value is non a function. For example:

var foo = undefined; foo();

This error typically occurs if y'all are trying to call a function in an object, but yous typed the name incorrect.

var x = document.getElementByID('foo');

Since object properties that don't exist are undefined by default, the above would consequence in this fault.

The other variations such as "number is not a function" occur when attempting to call a number similar information technology was a function.

How to gear up this error: Ensure the function name is right. With this error, the line number will normally betoken at the correct location.

Uncaught ReferenceError: Invalid left-hand 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 be assigned to.

The near mutual example of this mistake is with if-clauses:

if(doSomething() = 'somevalue')

In this example, the developer accidentally used a single equals instead of two. The message "left-hand side in consignment" is referring to the part on the left side of the equals sign, so similar yous tin meet in the above instance, the left-paw side contains something you can't assign to, leading to the error.

How to prepare this error: Brand certain you're not 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 non supported

Always caused by a round reference in an object, which is then passed into JSON.stringify.

var a = { }; var b = { a: a }; a.b = b; JSON.stringify(a);

Because both a and b in the to a higher place example have a reference to each other, the resulting object cannot be converted into JSON.

How to gear up this error: Remove circular references like in the example from any objects you want to convert into JSON.

Unexpected token ;

Related errors: Expected ), missing ) afterwards argument list

The JavaScript interpreter expected something, only information technology wasn't there. Typically caused by mismatched parentheses or brackets.

The token in this error tin can vary – it might say "Unexpected token ]" or "Expected {" etc.

How to fix this mistake: Sometimes the line number with this error doesn't indicate to the correct place, making it difficult to fix.

  • An error with [ ] { } ( ) is usually acquired by a mismatching pair. Bank check that all your parentheses and brackets have a matching pair. In this example, line number volition often point to something else than the problem character
  • Unexpected / is related to regular expressions. The line number for this will usually be right.
  • Unexpected ; is usually caused past having a ; inside an object or array literal, or within the argument listing of a function call. The line number will usually be correct for this example likewise

Uncaught SyntaxError: Unexpected token ILLEGAL

Related errors: Unterminated Cord Literal, Invalid Line Terminator

A string literal is missing the closing quote.

How to set this error: Ensure all strings have the right closing quote.

Uncaught TypeError: Cannot read property 'foo' of nil, Uncaught TypeError: Cannot read belongings 'foo' of undefined

Related errors: TypeError: someVal is null, Unable to get holding 'foo' of undefined or null reference

Attempting to read null or undefined as if it was an object. For example:

var someVal = null; console.log(someVal.foo);

How to fix this error: Usually caused by typos. Check that the variables used near the line number pointed by the fault are correctly named.

Uncaught TypeError: Cannot set holding 'foo' of zilch, Uncaught TypeError: Cannot set property 'foo' of undefined

Related errors: TypeError: someVal is undefined, Unable to set up property 'foo' of undefined or zilch reference

Attempting to write null or undefined every bit if it was an object. For example:

var someVal = zilch; someVal.foo = 1;

How to fix this error: This too is ordinarily caused by typos. Check the variable names nigh the line the error points to.

Uncaught RangeError: Maximum phone call stack size exceeded

Related errors: Uncaught exception: RangeError: Maximum recursion depth exceeded, also much recursion, Stack overflow

Ordinarily caused by a issues in program logic, causing infinite recursive part calls.

How to fix this error: Check recursive functions for bugs that could cause them to keep recursing forever.

Uncaught URIError: URI malformed

Related errors: URIError: malformed URI sequence

Caused by an invalid decodeURIComponent call.

How to set up this error: Check that the decodeURIComponent call at the error's line number gets right input.

XMLHttpRequest cannot load http://some/url/. No 'Access-Control-Allow-Origin' header is present on the requested resource

Related errors: Cross-Origin Request Blocked: The Same 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 it respects the same-origin policy. A good way to notice the offending lawmaking is to wait at the URL in the error 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 eleven

Ways the code called a role that you should not call at the current land. Occurs commonly with XMLHttpRequest, when attempting to phone call functions on information technology earlier information technology's gear up.

var xhr = new XMLHttpRequest(); xhr.setRequestHeader('Some-Header', 'val');

In this case, you would get the error because the setRequestHeader function tin only exist called after calling xhr.open.

How to fix this error: Wait at the code on the line pointed past the error and make sure it runs at the right time, or add together whatever necessary calls earlier information technology (such as xhr.open)

Conclusion

JavaScript has some of the almost unhelpful errors I've seen, with the exception of the notorious Expected T_PAAMAYIM_NEKUDOTAYIM in PHP. With more familiarity the errors kickoff to brand more sense. Modern browsers also help, as they no longer give the completely useless errors they used to.

What's the most confusing error y'all've seen? Share the frustration in the comments!

Want to acquire more about these errors and how to forestall them? Detect Problems in JavaScript Automatically with ESLint.

Website performance monitoring

Website performance monitoring

Jani Hartikainen

Well-nigh Jani Hartikainen

Jani Hartikainen has spent over ten years building web applications. His clients include companies like Nokia and hot super secret startups. When not programming or playing games, Jani writes nearly JavaScript and high quality lawmaking on his site.

peakgoon1974.blogspot.com

Source: https://davidwalsh.name/fix-javascript-errors

Posting Komentar untuk "App.js:115 Uncaught Typeerror: Cannot Read Property 'insertadjacenthtml' of Null"