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):
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.