Typeerror Cannot Read Property 'indexof' of Undefined Node Js

JavaScript Errors and How to Prepare Them

JavaScript tin be a nightmare to debug: Some errors it gives can exist very difficult to sympathize at kickoff, and the line numbers given aren't e'er helpful either. Wouldn't it be useful to take a listing where you could look to find out what they mean and how to fix them? Hither yous become!

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

How to read errors?

Before the list, let'south quickly look at the structure of an error message. Understanding the construction helps understand the errors, and you'll have less trouble if you run into any errors non listed here.

A typical error from Chrome looks like this:

Uncaught TypeError: undefined is not a function

The structure of the fault is as follows:

  1. Uncaught TypeError: This part of the bulletin is commonly not very useful. Uncaught means the fault was non caught in a catch argument, and TypeError is the error'south proper noun.
  2. undefined is not a function: This is the message part. With error messages, yous have to read them very literally. For example in this case it literally means that the lawmaking attempted to use 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 do not ever include the first part, and contempo versions of Internet Explorer likewise give simpler errors than Chrome – only 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 function, object is not a function, cord is non a function, Unhandled Error: 'foo' is non a office, Function Expected

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

var foo = undefined; foo();

This error typically occurs if you lot are trying to call a office in an object, merely you typed the proper name wrong.

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

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

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

How to fix this mistake: Ensure the part name is correct. With this fault, the line number will usually point at the right location.

Uncaught ReferenceError: Invalid left-hand side in consignment

Related errors: Uncaught exception: ReferenceError: Cannot assign to 'functionCall()', Uncaught exception: ReferenceError: Cannot assign to 'this'

Acquired by attempting to assign a value to something that cannot be assigned to.

The well-nigh mutual example of this error is with if-clauses:

if(doSomething() = 'somevalue')

In this instance, the programmer accidentally used a unmarried 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 tin can see in the above example, the left-hand side contains something you can't assign to, leading to the error.

How to set this error: Make sure 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, Round reference in value statement not supported

Ever caused 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 be converted into JSON.

How to set this error: Remove round references similar in the example from any objects you want to convert into JSON.

Unexpected token ;

Related errors: Expected ), missing ) afterward argument list

The JavaScript interpreter expected something, but it wasn't there. Typically caused by mismatched parentheses or brackets.

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

How to gear up this error: Sometimes the line number with this error doesn't point to the correct place, making information technology difficult to ready.

  • An error with [ ] { } ( ) is usually acquired past a mismatching pair. Check that all your parentheses and brackets have a matching pair. In this case, line number will often point to something else than the problem character
  • Unexpected / is related to regular expressions. The line number for this will unremarkably exist correct.
  • Unexpected ; is usually caused by having a ; inside an object or array literal, or inside the argument list of a function telephone call. The line number will usually be correct for this instance as well

Uncaught SyntaxError: Unexpected token ILLEGAL

Related errors: Unterminated String Literal, Invalid Line Terminator

A string literal is missing the closing quote.

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

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

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

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

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

How to set up this error: Usually acquired by typos. Check that the variables used near the line number pointed by the mistake are correctly named.

Uncaught TypeError: Cannot set property 'foo' of null, Uncaught TypeError: Cannot set property 'foo' of undefined

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

Attempting to write null or undefined as if it was an object. For instance:

var someVal = null; someVal.foo = i;

How to fix this error: This too is unremarkably caused 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, also much recursion, Stack overflow

Normally caused by a bug in program logic, causing infinite recursive role calls.

How to fix this error: Bank check recursive functions for bugs that could crusade them to keep recursing forever.

Uncaught URIError: URI malformed

Related errors: URIError: malformed URI sequence

Caused by an invalid decodeURIComponent call.

How to prepare this fault: Check that the decodeURIComponent call at the error's line number gets correct input.

XMLHttpRequest cannot load http://some/url/. No 'Admission-Control-Allow-Origin' header is nowadays on the requested resources

Related errors: Cantankerous-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://some/url/

This error is always acquired past the usage of XMLHttpRequest.

How to fix this mistake: Ensure the request URL is correct and it respects the same-origin policy. A skilful way to observe the offending lawmaking is to look at the URL in the error bulletin and notice 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 11

Means the code called a office that you lot should non call at the current country. Occurs commonly with XMLHttpRequest, when attempting to call functions on it earlier it's prepare.

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

In this case, you would get the error because the setRequestHeader function can only be called afterwards calling xhr.open.

How to fix this error: Look at the code on the line pointed by the error and make sure it runs at the right time, or add whatever necessary calls before 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 beginning to make more sense. Mod browsers as well help, as they no longer give the completely useless errors they used to.

What'due south the most confusing mistake you've seen? Share the frustration in the comments!

Want to learn more than about these errors and how to prevent them? Find Problems in JavaScript Automatically with ESLint.

Website performance monitoring

Website performance monitoring

Jani Hartikainen

About Jani Hartikainen

Jani Hartikainen has spent over 10 years building web applications. His clients include companies like Nokia and hot super hole-and-corner startups. When not programming or playing games, Jani writes well-nigh JavaScript and loftier quality code on his site.

glanzgened2001.blogspot.com

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

Related Posts

0 Response to "Typeerror Cannot Read Property 'indexof' of Undefined Node Js"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel