Q:

How to fix "Cannot read property of undefined" error

I'm trying to render filtered data using the following code:
const notes = [{
    title: "Order cat food",
    body: "Get 1 kg."
},
{
    title: "Clean kitchen",
    body: "The fridge door."
},
{
    title: "Buy groceries",
    body: "Remember the yellow and red pepper."
},
{
    title: "Go to the gym",
    body: "Exercise for 2hrs."
},
{
    text: "Make porridge",
    body: "Remember to put lime and honey."
}]

const allfilters = {
    searchText: ""
}

const renderNotes = function(notes, filters) {
    const filteredNotes = notes.filter(function (note) {
        return note.title.toLowerCase().includes(filters.searchText.toLowerCase());
    })
    console.log(filteredNotes);
}

renderNotes(notes, allfilters);

document.querySelector('#to-do').addEventListener('input', function(e) {
   allfilters.searchText = e.target.value;
   renderNotes(notes, allfilters);
})

After running, I get the error, "cannot read property 'lowercase' of undefined."
What am I missing?

A:

{
    text: "Make porridge",
    body: "Remember to put lime and honey."
}

Your last array item has no title, instead it has text, so in
note.title.toLowerCase()

for that array item note.title is undefined. toLowerCase() does not exist on undefined.

