如何修复“无法读取未定义的属性”错误

时间:2019-01-17 13:58:32

标签: javascript

我正在尝试使用以下代码呈现过滤后的数据:

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);
})

运行后,出现错误“无法读取未定义的属性'lowercase'”。

我想念什么?

1 个答案:

答案 0 :(得分:7)

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

您的最后一个数组项没有title,而是text,所以在

note.title.toLowerCase()
该数组项note.title

undefinedtoLowerCase()上不存在undefined