Typeerror: Cannot Read Property 'includes' of Null

JavaScript Errors and How to Set Them

JavaScript can be a nightmare to debug: Some errors it gives can exist very hard to understand at first, and the line numbers given aren't always helpful either. Wouldn't it be useful to accept a listing where you could expect to find out what they mean and how to fix them? Here you get!

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

How to read errors?

Before the list, let'southward quickly wait at the structure of an error message. Understanding the structure helps understand the errors, and you'll have less trouble if yous run into whatever errors not listed hither.

A typical error from Chrome looks like this:

Uncaught TypeError: undefined is non a part

The structure of the mistake is every bit follows:

  1. Uncaught TypeError: This function of the message is ordinarily not very useful. Uncaught ways the error was not caught in a take hold of statement, and TypeError is the error's proper name.
  2. undefined is not a office: This is the bulletin role. With fault messages, you have to read them very literally. For example in this instance it literally means that the code attempted to employ undefined similar it was a function.

Other webkit-based browsers, similar Safari, give errors in a like format to Chrome. Errors from Firefox are like, but do not e'er include the first part, and recent versions of Net Explorer likewise give simpler errors than Chrome – just in this instance, simpler does not always hateful meliorate.

Now onto the actual errors.

Uncaught TypeError: undefined is not a function

Related errors: number is not a role, object is not a office, string is not a function, Unhandled Error: 'foo' is not a role, Role Expected

Occurs when attempting to telephone call a value similar a role, where the value is not a function. For example:

var foo = undefined; foo();

This fault typically occurs if you are trying to call a part in an object, only yous typed the name incorrect.

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

Since object properties that don't exist are undefined by default, the higher up would result in this error.

The other variations such as "number is non a function" occur when attempting to call a number like it was a function.

How to fix this error: Ensure the function name is right. With this mistake, the line number will usually point at the right 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 most common example of this error is with if-clauses:

if(doSomething() = 'somevalue')

In this example, the programmer accidentally used a unmarried equals instead of ii. The message "left-hand side in assignment" is referring to the role on the left side of the equals sign, so like you can see in the above example, the left-paw side contains something you can't assign to, leading to the mistake.

How to gear up this error: Brand certain you're non attempting to assign values to function results or to the this keyword.

Uncaught TypeError: Converting round structure to JSON

Related errors: Uncaught exception: TypeError: JSON.stringify: Not an acyclic Object, TypeError: cyclic object value, Circular reference in value statement not supported

E'er caused past a circular reference in an object, which is and so passed into JSON.stringify.

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

Considering both a and b in the above example have a reference to each other, the resulting object cannot exist converted into JSON.

How to gear up this error: Remove circular references like in the case from whatsoever objects you lot desire to convert into JSON.

Unexpected token ;

Related errors: Expected ), missing ) after argument list

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

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

How to ready this fault: Sometimes the line number with this fault doesn't bespeak to the correct place, making information technology difficult to fix.

  • An fault with [ ] { } ( ) is ordinarily caused by a mismatching pair. Check that all your parentheses and brackets have a matching pair. In this case, line number volition ofttimes point to something else than the problem character
  • Unexpected / is related to regular expressions. The line number for this volition usually exist correct.
  • Unexpected ; is usually caused by having a ; inside an object or assortment literal, or within the statement list of a part telephone call. The line number will usually be right for this case too

Uncaught SyntaxError: Unexpected token ILLEGAL

Related errors: Unterminated String Literal, Invalid Line Terminator

A string literal is missing the closing quote.

How to gear up this error: Ensure all strings have the correct endmost quote.

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

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

Attempting to read cipher or undefined every bit if it was an object. For example:

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

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

Uncaught TypeError: Cannot ready property 'foo' of zip, Uncaught TypeError: Cannot fix holding 'foo' of undefined

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

Attempting to write null or undefined every bit if information technology was an object. For case:

var someVal = zippo; someVal.foo = 1;

How to fix this error: This too is unremarkably acquired by typos. Check 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

Commonly acquired by a issues in program logic, causing infinite recursive office calls.

How to gear up this mistake: 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 fix this mistake: Bank check that the decodeURIComponent phone call at the fault's line number gets correct input.

XMLHttpRequest cannot load http://some/url/. No 'Admission-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 set this error: Ensure the request URL is correct and it respects the same-origin policy. A good way to find the offending code is to look at the URL in the error bulletin and find it from your code.

InvalidStateError: An effort was made to use an object that is not, or is no longer, usable

Related errors: InvalidStateError, DOMException code 11

Ways the lawmaking called a office that you should non call at the current land. Occurs usually with XMLHttpRequest, when attempting to call functions on it before information technology'south gear up.

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

In this instance, you would get the error because the setRequestHeader function tin can but exist called after calling xhr.open.

How to ready this error: Look at the code on the line pointed by the error and make sure it runs at the correct time, or add whatever necessary calls earlier it (such every bit xhr.open)

Conclusion

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

What'southward the near confusing error you've seen? Share the frustration in the comments!

Want to larn more virtually these errors and how to prevent them? Notice Problems in JavaScript Automatically with ESLint.

Website performance monitoring

Website performance monitoring

Jani Hartikainen

About Jani Hartikainen

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

Typeerror: Cannot Read Property 'includes' of Null

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

0 Response to "Typeerror: Cannot Read Property 'includes' of Null"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel