Почему Fetch всегда возвращает пустое или неопределенное в моем шахте входа в систему? [закрыто]Html

Программисты Html
Ответить Пред. темаСлед. тема
Anonymous
 Почему Fetch всегда возвращает пустое или неопределенное в моем шахте входа в систему? [закрыто]

Сообщение Anonymous »

Я создаю простую страницу входа в систему, используя JS, которая получает пользовательские данные из https://jsonplaceholder.typicode.com/users.
Я пытаюсь проверить, существует ли введенное имя пользователя или электронная почта, но Fetch () всегда возвращает пустой результат или не определен.const res = await fetch('https://jsonplaceholder.typicode.com/users');
const users = await res.json();
const found = users.find(u => u.username === input || u.email === input);
< /code>





Simple Login

body {
font-family: sans-serif;
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
margin: 0;
background: #eee;
}

.box {
background: #fff;
padding: 25px 30px;
border-radius: 8px;
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
text-align: center;
}

input {
padding: 10px;
width: 220px;
font-size: 15px;
border: 1px solid #ccc;
border-radius: 5px;
}

button {
padding: 10px 16px;
font-size: 15px;
margin-left: 10px;
background: #007bff;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
}

button:hover {
background: #0056b3;
}

#message {
color: red;
margin-top: 15px;
}

#result {
color: green;
margin-top: 10px;
}





Log In






async function login() {
const input = document.getElementById('userInput');
const value = input.value.trim().toLowerCase();
const message = document.getElementById('message');
const result = document.getElementById('result');

message.textContent = '';
result.textContent = '';

if (value.length < 3) {
message.textContent = 'Too short. Min 3 characters.';
return;
}

try {
const res = await fetch('https://jsonplaceholder.typicode.com/users');
const users = await res.json();

const found = users.find(u =>
u.username.toLowerCase() === value || u.email.toLowerCase() === value
);

if (found) {
localStorage.setItem('user', JSON.stringify(found));
result.textContent = `hello ${found.username.toLowerCase()}...`;
} else {
message.textContent = "We don;t have this USerd";
}
} catch (err) {
message.textContent = 'Could not connect.';
console.log(err);
}
}






Подробнее здесь: https://stackoverflow.com/questions/796 ... in-checker
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «Html»