Я немного запутался в том, как отображать ошибку, если в файле json не найден заголовок. Вот код, который я пробовал. Все работает отлично, но когда я использую if (searchName! = Todo.name), тогда в результате отображается только ошибка независимо от того, пишется имя или нет. Пожалуйста, помогите мне. Я буду признателен.
const list = document.getElementById('list');
const userAction = async (event) => {
event.preventDefault();
const searchName = document.getElementById('searchName').value.trim();
fetch('https://jsonplaceholder.typicode.com/todos')
.then(response => response.json())
.then(todos => {
todos.forEach((todo) => {
if(searchName == todo.title){
const li = document.createElement('li');
li.textContent = `${todo.userId} ${todo.id} ${todo.title} ${todo.completed}`;
list.appendChild(li);
} else if(searchName != todo.title)
{
const li = document.createElement('li');
list.textContent = `Search Result not found`;
list.appendChild(li);
}
});
})
}
<form method="POST">
<input type="text" id="searchName" class="form-control" placeholder="Lorem name">
<button onclick="userAction(event)" class="search-icon"><img src="images/icon.png" alt="submit"></button>
</form>
<ul id="list">
</ul>