Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

707
Views
ReferenceError: buscar no está definido

Tengo este error cuando compilo mi código en node.js, ¿cómo puedo solucionarlo?

ReferenceError: la búsqueda no está definida

ingrese la descripción de la imagen aquí

Esta es la función que estoy haciendo, se encarga de recuperar información de una base de datos de películas en específico.

 function getMovieTitles(substr){ pageNumber=1; let url = 'https://jsonmock.hackerrank.com/api/movies/search/?Title=' + substr + "&page=" + pageNumber; fetch(url).then((resp) => resp.json()).then(function(data) { let movies = data.data; let totPages = data.total_pages; let sortArray = []; for(let i=0; i<movies.length;i++){ sortArray.push(data.data[i].Title); } for(let i=2; i<=totPages; i++){ let newPage = i; let url1 = 'https://jsonmock.hackerrank.com/api/movies/search/?Title=' + substr + "&page=" + newPage; fetch(url1).then(function(response) { var contentType = response.headers.get("content-type"); if(contentType && contentType.indexOf("application/json") !== -1) { return response.json().then(function(json) { //console.log(json); //uncomment this console.log to see the JSON data. for(let i=0; i<json.data.length;i++){ sortArray.push(json.data[i].Title); } if(i==totPages)console.log(sortArray.sort()); }); } else { console.log("Oops, we haven't got JSON!"); } }); } }) .catch(function(error) { console.log(error); }); }
over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

La API de búsqueda no está implementada en Node.

Necesita usar un módulo externo para eso, como node-fetch .

Instálalo en tu aplicación Node así

 npm install node-fetch

luego coloque la línea a continuación en la parte superior de los archivos donde está utilizando la API de búsqueda:

 import fetch from "node-fetch";
over 4 years ago · Santiago Trujillo Report

0

Esta es una solución sucia rápida, intente eliminar este uso en el código de producción.

Si fetch tiene que ser accesible con un alcance global

 import fetch from 'node-fetch' globalThis.fetch = fetch
over 4 years ago · Santiago Trujillo Report

0

Puedes usar cross-fetch de @lquixada

Plataforma independiente: navegadores, nodo o reaccionar nativo

Instalar en pc

 npm install --save cross-fetch

Uso

con promesas:

 import fetch from 'cross-fetch'; // Or just: import 'cross-fetch/polyfill'; fetch('//api.github.com/users/lquixada') .then(res => { if (res.status >= 400) { throw new Error("Bad response from server"); } return res.json(); }) .then(user => { console.log(user); }) .catch(err => { console.error(err); });

Con asíncrono/espera:

 import fetch from 'cross-fetch'; // Or just: import 'cross-fetch/polyfill'; (async () => { try { const res = await fetch('//api.github.com/users/lquixada'); if (res.status >= 400) { throw new Error("Bad response from server"); } const user = await res.json(); console.log(user); } catch (err) { console.error(err); } })();
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!