Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

174
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda