-1

I am trying to convert an object attribute into all lowercase and I am getting the error: Cannot read property of undefined (reading 'toLowerCase'). When I console logged the object (called preferenceList), it turns out something like the picture below. I only want 'preferences' to be converted into lowercase (and replace all commas with space):

enter image description here

My code is like this:

function checkSimilarity(preferenceList, projectList) {

    if(preferenceList.preferences){

      //this is where the error comes from
      const text11 = (preferenceList.preferences).toLowerCase().replace(/,/, " ");

      for (let j = 0; j < projectList.length; j++) {

          const text22 = projectList[j].project_information.toLowerCase();

        simi = getSimilarityScore(
          textCosineSimilarity(text11, text22)
        );

        similarityList.push(simi);
      }
      
      return similarityList;
    }
    else{
      console.log("helo");
    }
      
  }

This is how I fetch preferenceList and called the function with useEffect:

const [projectList, setProjectList] = useState([]);

  const [preferences, setPreference] = useState(false);
  const [preferenceList, setPreferenceList] = useState([]);

useEffect(()=>{
      Axios.get(`http://localhost:3001/profile/${userId}`,{
            id: userId,
        })
        .then((res) => {
    
            if(res.data.preferences === null){
                setPreference(false);
            }
            else{
                setPreference(true);
                setPreferenceList(res.data);
              
            }
        })
  })

  useEffect(()=>{

    checkSimilarity(preferenceList, projectList);

  });

Can anyone help, what is really wrong with the toLowerCase() function? Thank you in advance.

red17
  • 81
  • 6
  • You can try checking if preferenceList.preferences exists. either by an if statement: if (preferenceList.preferences) { } or using ? preferenceList.preferences?.toLowerCase().replace(/,/, " ") – Erik May 22 '22 at 14:50
  • @Erik weirdly enough, it counts as else, which means 'preferences' does not exist here, eventhough it does – red17 May 22 '22 at 14:56
  • how is the function getting called? `preferenceList` is an argument to the function, so we have no way of diagnosing why it has a particular value if we can't see what it's called with – Robin Zigmond May 22 '22 at 14:58
  • @Erik I updated my question, please do check it out – red17 May 22 '22 at 15:05
  • `Axios.get()` is asynchronous, the `checkSimilarity()` call will happen earlier than `setPreferenceList()`. – tevemadar May 22 '22 at 15:11
  • @tevemadar I console logged preferenceList.preferences, and it contains the result from the picture above – red17 May 22 '22 at 15:20
  • Check with `console.log(JSON.stringify(preferenceList));` then. https://stackoverflow.com/questions/24175017/google-chrome-console-log-inconsistency-with-objects-and-arrays – tevemadar May 22 '22 at 15:55
  • @tevemadar that did the trick, thank you! It now counts into the first if, but the error ```Cannot read property of undefined (toLowerCase)``` is still happening – red17 May 22 '22 at 16:15

0 Answers0