Changing this if condition from this:
if (props.items.length === 0) {
To
if (!props.items?.length) {
Should work.
Similarly if using for places,you can check using ternary operator if length exists in array of places.
How? Because items could possibly be null or undefined from apis, so the length property on array might be missing.
Moreover, using ! Not operator would make this condition true if length of items is 0 or is null or undefined or any falsy value
Changing this if condition from this:
if (props.items.length === 0) {
To
if (!props.items?.length) {
Should work.
Similarly if using for places,you can check using ternary operator if length exists in array of places.
How? Because items could possibly be null or undefined from apis, so the length property on array might be missing.
Moreover, using ! Not operator would make this condition true if length of items is 0 or is null or undefined or any falsy value
The other answers are focusing on prop.items list but in your question you mention that you get the exception when trying to access the length of the places inside users.
You are getting the error message because some users may not have any places listed inside them, hence, no places list -> no length property.
To fix this, you need to check if the places list exists and then access its length:
placeCount={ user.places ? user.places.length : 0 }
Here, we use a ternary operator to check if user.places exists, then we use the length, else use zero.
Edit: As pointed out by Phil in the comments below, you can also use coalescing operator, which is much cleaner, like this:
placeCount={ user.places?.length ?? 0 }
The syntax simply translates as if user.places has a value for length, then use that value, else use zero as a default value.
angular - Jasmine unit tests - TypeError: Cannot read properties of undefined (reading 'pipe') - Stack Overflow
typeerror - JavaScript - Cannot read properties of undefined (reading 'length') - (Code does work and provided) - Stack Overflow
TypeError: Cannot read property 'length' of undefined, when karma tests re-load
angular - Karma-Jasmine: TypeError: Cannot read properties of undefined (reading 'get') - Stack Overflow
Cannot read property 'length' of undefined comes when it is not able to find variable of certain type(In your case a string) to call the function length. In your case arrayWord[i].length is not a proper string for the last condition of your check as there is no element arrayWord[arrayWord.length] present in the array. That's why arrayWord[i].length is giving you an error for your last iteration. Just change i <= arrayWord.length to i < arrayWord.length
function find(par) {
let arrayWord = par.split(" ");
let longestWord = "";
for (let i = 0; i <arrayWord.length; i++) {
if (longestWord.length < arrayWord[i].length) {
longestWord = arrayWord[i]
}
}
return longestWord;
}
Edits: Changes made as suggested by RobG
Just change condition <= to < and try
function find(par) {
let arrayWord = par.split(" ");
let longestWord = "";
for (let i = 0; i < arrayWord.length; i++) {
if (longestWord.length < arrayWord[i].length) {
longestWord = arrayWord[i]
}
}
return longestWord;
}
console.log(find("Find the longest word"));
Run code snippetEdit code snippet Hide Results Copy to answer Expand
The id of the input seems is not WallSearch. Maybe you're confusing that name and id. They are two different properties. name is used to define the name by which the value is posted, while id is the unique identification of the element inside the DOM.
Other possibility is that you have two elements with the same id. The browser will pick any of these (probably the last, maybe the first) and return an element that doesn't support the value property.
perhaps, you can first determine if the DOM does really exists,
function walkmydog() {
//when the user starts entering
var dom = document.getElementById('WallSearch');
if(dom == null){
alert('sorry, WallSearch DOM cannot be found');
return false;
}
if(dom.value.length == 0){
alert("nothing");
}
}
if (document.addEventListener){
document.addEventListener("DOMContentLoaded", walkmydog, false);
}
I am getting a type error in my code as given in the title:
TypeError: Cannot read properties of undefined (reading 'length')
I have researched the error message and can't figure out what's going on.
Here is the full code, specific block throwing the error is given below.
// All valid credit card numbers
const valid1 = [4, 5, 3, 9, 6, 7, 7, 9, 0, 8, 0, 1, 6, 8, 0, 8]
const valid2 = [5, 5, 3, 5, 7, 6, 6, 7, 6, 8, 7, 5, 1, 4, 3, 9]
const valid3 = [3, 7, 1, 6, 1, 2, 0, 1, 9, 9, 8, 5, 2, 3, 6]
const valid4 = [6, 0, 1, 1, 1, 4, 4, 3, 4, 0, 6, 8, 2, 9, 0, 5]
const valid5 = [4, 5, 3, 9, 4, 0, 4, 9, 6, 7, 8, 6, 9, 6, 6, 6]
// All invalid credit card numbers
const invalid1 = [4, 5, 3, 2, 7, 7, 8, 7, 7, 1, 0, 9, 1, 7, 9, 5]
const invalid2 = [5, 7, 9, 5, 5, 9, 3, 3, 9, 2, 1, 3, 4, 6, 4, 3]
const invalid3 = [3, 7, 5, 7, 9, 6, 0, 8, 4, 4, 5, 9, 9, 1, 4]
const invalid4 = [6, 0, 1, 1, 1, 2, 7, 9, 6, 1, 7, 7, 7, 9, 3, 5]
const invalid5 = [5, 3, 8, 2, 0, 1, 9, 7, 7, 2, 8, 8, 3, 8, 5, 4]
// Can be either valid or invalid
const mystery1 = [3, 4, 4, 8, 0, 1, 9, 6, 8, 3, 0, 5, 4, 1, 4]
const mystery2 = [5, 4, 6, 6, 1, 0, 0, 8, 6, 1, 6, 2, 0, 2, 3, 9]
const mystery3 = [6, 0, 1, 1, 3, 7, 7, 0, 2, 0, 9, 6, 2, 6, 5, 6, 2, 0, 3]
const mystery4 = [4, 9, 2, 9, 8, 7, 7, 1, 6, 9, 2, 1, 7, 0, 9, 3]
const mystery5 = [4, 9, 1, 3, 5, 4, 0, 4, 6, 3, 0, 7, 2, 5, 2, 3]
// An array of all the arrays above
const batch = [valid1, valid2, valid3, valid4, valid5, invalid1, invalid2, invalid3, invalid4, invalid5, mystery1, mystery2, mystery3, mystery4, mystery5]
// Add your functions below:
const validateCred = (arr) => {
let newArr = [];
for (let i = arr.length - 1; i >= 0; i--){
let sec = arr[i];
if (((arr.length - 1) - i) % 2 === 1) {
let num = arr[i] * 2;
if (num > 9) {
num -= 9;
}
newArr.push(num);
} else {
newArr.push(sec);
}
}
let sum = newArr.reduce((partialSum, a) => partialSum + a, 0)
if (sum % 10 === 0) {
return 'Valid';
} else {
return 'Invalid';
}
}
const findInvalidCards = (arr) => {
let invArr = []
for (let i = 0; i < arr.length; i++) {
if (validateCred(arr[i]) === 'Invalid') {
invArr.push(arr[i])
}
}
return invArr;
}
let invArray = findInvalidCards();
console.log(invArray);
const cardFunc = (list) => {
let compArr = [];
switch (list) {
case (list[0] === 3):
compArr.push('Amex');
case (list[0] === 4):
compArr.push('Visa');
case (list[0] === 5):
compArr.push('Mastercard');
case (list[0] === 6):
compArr.push('Discover');
}
let filterList = (arr) => {
return arr.filter((index, item) => arr.indexOf(item) === index);
}
return filterList;
}This is the block that is throwing the error.
const findInvalidCards = (arr) => {
let invArr = []
for (let i = 0; i < arr.length; i++) {
if (validateCred(arr[i]) === 'Invalid') {
invArr.push(arr[i])
}
}
return invArr;
}Any ideas? Thanks in advance!
the (arr) argument is null/undefined when this function is called. where you are calling findInvalidCards(), make sure you are passing in the array, and try doing a log statement just before the call to see if it’s null or not, which would be a symptom of a problem somewhere else
Your arr is undefined, impossible to say why just from this code. Maybe you don't pass it to this function wherever you call this from, but who knows.