Warning: session_start(): open(/tmp/sess_566dde716259b69df164ed45731085ac, O_RDWR) failed: Disk quota exceeded (122) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: session_start(): Failed to read session data: files (path: /tmp) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_mkdir() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 562

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230
Cannot read property 'includes' of Undefined in JavaScript - How to solve it? - LearnShareIT

Cannot read property ‘includes’ of Undefined in JavaScript – How to solve it?

Cannot read property 'includes' of Undefined in JavaScript

“Cannot read property ‘includes’ of Undefined” in JavaScript is one of the most common JavaScript errors today. This article will help you understand this error quickly and we must check the correct data type call to that property and assign a value for it.

How does the error “Cannot read property ‘includes’ of Undefined” in JavaScript happen?

The “Cannot read property ‘includes’ of Undefined” error happens when accessing the includes() method in an undefined object.

In Javascript, the ‘include’ attribute is only used in array and string data types, so if you call the method on a newly declared, undefined value, it will cause an error like the following example.

Code sample:

// Array variable is undefined
let myArr;

console.log(myArr.includes('learn')); // The error appears in this line

Output:

TypeError: Cannot read properties of undefined (reading 'includes')

Another example:

let students = [
    {name: 'Lisa', age: 20, address: 'Korea'},
    {name: 'Misa', age: 22, address: 'Singapore'},
    {name: 'Anna', age: 21, address: 'America'}
]

// Find the address America is in the array or not
console.log(students.address.includes('America'));

In this example, the error occurs because the student array has no address attribute, so student.address is undefined and cannot call the includes() method.

Output:

TypeError: Cannot read properties of undefined (reading 'includes')

How to solve this error?

Check the includes() method is called in the correct data type

When encountering this error, you should check if the object you need has data. If not, assign the data to the “undefined” object.

In simple situations, the root cause is forgetting to assign data to the variable before using it. Therefore, you need to add a statement to assign data to the variable.

Code sample:

// Assign values to array and string variables
let myArr = ['Learn', 'Share', 'IT'];
let myStr = 'I love LearnShareIT';
 
// Check if the string to look for is in the array and string
console.log(myArr.includes('share'));
console.log(myArr.includes('IT'));
console.log(myStr.includes('Learn'));

Output

false
true
true

Access to a specific object in an array

If you call the include method on an undefined object in an array, you should access the correct object to look for. See the following example to understand how to fix this error.

Code sample:

let students = [
    {name: 'Lisa', age: 20, address: 'Korea'},
    {name: 'Misa', age: 22, address: 'Singapore'},
    {name: 'Anna', age: 21, address: 'America'}
]

// Access each object in the array to find the address
console.log(students[0].address.includes('America'));
console.log(students[1].address.includes('America'));
console.log(students[2].address.includes('America'));

Output

false
false
true

Summary

Don’t worry about getting the error “Cannot read property ‘includes’ of Undefined” in JavaScript. Check whether the includes() method was called on which object has been defined or not to have the best solution. I have helped you fix this error in two simple ways, apply to your case. Good luck with your fix!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *