Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

164
Visualizações
fetch() catch error: show "no result" when there's no match in API

I'm trying to tell the user "No result!" when there's no match with their search. But i couldn't make it work.

enter code here

        let END_POINT = `https://api.tvmaze.com/search/shows?q=${inputValue}`
        fetch(END_POINT)
            .then(response => {
                if (response.status === 200) {
                    return response.json()
                }
                // throw new Error("No result!")
                return Promise.reject(response)
            })
            .then(data => {
                //initial cotent in the content container
                showContent(data[0])
                //forEach, display content for each click
                data.forEach(tvShow => {
                    displayContent(tvShow)
                })
            })
            .catch(error => {
                console.log(error)
            })
<div class="header">
            <div class="title">TV shows search engine</div>
            <div class="form">
                <form action="">
                    <input type="text" name="" id="" placeholder="Dune">
                    <button type="submit">Search</button>
                </form>
                <p class="error">No result!</p>
            </div>
        </div>

Any advise how to solve this?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Check is data.length > 0 before printing results:

let END_POINT = `https://api.tvmaze.com/search/shows?q=${inputValue}`
fetch(END_POINT)
    .then(response => {
        if (response.status === 200) {
            return response.json()
        }
        // throw new Error("No result!")
        return Promise.reject(response)
    })
    .then(data => {
        if(data.length> 0) {
                //initial cotent in the content container
            showContent(data[0])
            //forEach, display content for each click
            data.forEach(tvShow => {
                displayContent(tvShow)
            });
        } else {
            console.log("No result !");
        }
    })
    .catch(error => {
        console.log(error)
    })
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda