Browse Source

🥅 Handle server errors

master
Tiana Lemesle 9 months ago
parent
commit
b619eef51b
1 changed files with 5 additions and 1 deletions
  1. +5
    -1
      public/assets/js/index.js

+ 5
- 1
public/assets/js/index.js View File

@ -36,6 +36,7 @@ const search = (query, callback) => {
headers: { 'content-type': 'application/json' },
body: JSON.stringify(query)
}).then(res => res.json()).then(res => {
if(res.error) return callback();
if(res.length === 0 && query.number !== 120) return search({ ...query, number: 120 }, callback);
const r = [];
for(let i = 0; i < res.length; i++){
@ -60,7 +61,7 @@ const search = (query, callback) => {
if(r.length === 0 && query.number !== 120) return search({ ...query, number: 120 }, callback);
lastQuery = query.name;
callback(r);
});
}).catch(() => callback());
};
/*
@ -131,6 +132,9 @@ searchButton.onclick = () => {
document.querySelector('.output2__data').innerHTML = '';
// Send query
search(query, res => {
if(typeof res === 'undefined'){
return outputMessage.textContent = 'Oops! Something went wrong.';
}
if(res.length === 0){
return outputMessage.textContent = 'No result.';
}


Loading…
Cancel
Save