0

i have checked the node list before adding the 'fetchValues' function to make sure that it have a value , but when i add the function , this error shows up: Uncaught TypeError: Cannot read properties of undefined (reading 'value')

i dont understand why the code runs with no issues and the opposet when i add the function ?

const fetchValues = (attrs, ...nodeLists) => {
    let elemsAttrCount = nodeLists.length;
    let elemsDataCount = nodeLists[0].length;
    let tempDataArr = [];

    //first loop deals with the no of repeaters value
    for(let i = 0; i<elemsDataCount; i++ ){
        let dataObj = {}; // creating an empty obj to fill the data
        //second loop feletej th0eajh<eelemeaters value or attributes
        for (let j = 0; j < elemsAttrCount; i++ ) {
            // setting the key name for the obj and fill it with data
            dataObj ['${attrs[j]}'] = nodeLists[j][i].value;
        }
        tempDataArr.push(dataObj);
    }
    return tempDataArr;
}

and here is the code when a checked it , before and after the function :

// before
    let acheivementsTitleElem = document.querySelectorAll('.acheiv-title'),
    acheivementsDescriptionElem = document.querySelectorAll('.acheiv-description');
    console.log(acheivementsTitleElem,acheivementsDescriptionElem);
// after
    let acheivementsTitleElem = document.querySelectorAll('.acheiv-title'),
    acheivementsDescriptionElem = document.querySelectorAll('.acheiv-description');
    console.log(fetchValues(['acheiv-title','acheiv-description'],acheivementsTitleElem,acheivementsDescriptionElem));

i tried to understand it but i didn't find any logic reason of not finding the value of the node list

6
  • Not related to the error, but ['${attrs[j]}'] should be [attrs[j]]
    – Barmar
    Commented Mar 29 at 17:41
  • The error implies that the elements of nodeLists aren't all the same length. You'll get that error if any of them are shorter than nodeLists[0].
    – Barmar
    Commented Mar 29 at 17:42
  • What does console.log(acheivementsTitleElem.length, acheivementsDescriptionElem.length) show?
    – Barmar
    Commented Mar 29 at 17:44
  • It shows 1 1 , the DevTools shows that the error is in : "nodeList[ j ][ i ].value " Wich is exactly in "value" Commented Mar 29 at 18:14
  • 1
    Please post a minimal reproducible example. You can use a Stack Snippet to make it executable.
    – Barmar
    Commented Mar 29 at 19:12

0