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

333
Views
¿Cómo puedo obtener los resultados reales de este código?

Cuando console.log . registro la salida de getSpecificMonitorNews(url); Obtengo los resultados reales. Pero cuando uso return await getSpecificMonitorNews(url); , los primeros resultados se sobrescriben con el último resultado recibido.

 let newsContents = { title: "", imageSrc: "", contents: "", date: "", }; async function getSpecificMonitorNews(url) { let monitorBaseUrl = "https://www.monitor.co.ug"; url = monitorBaseUrl + url; console.log(url) const data = await fetchPage(url); let $ = cheerio.load(data); newsContents.title = $(".title-medium").text(); newsContents.imageSrc = monitorBaseUrl + $(".lazy-img-container img").attr("src"); newsContents.contents = $(".paragraph-wrapper > p").text(); newsContents.date = $("time").text(); return newsContents; } async function getMonitorNews(urlNews) { return await Promise.all( urlNews.map(async (url) => { return await getSpecificMonitorNews(url); }) ); }
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

newsContents es una variable global por lo que su valor cambia. Está devolviendo una referencia a ese objeto global, no una versión copiada (y no hay necesidad de copiarlo si solo lo trae dentro de la función).

Entonces, puede mover newsContents dentro de la función si no lo necesita en otro lugar:

 async function getSpecificMonitorNews(url) { let newsContents = { title: "", imageSrc: "", contents: "", date: "", }; // rest of code } // rest of code

O haga una copia de la variable newsContents

 // rest of code async function getSpecificMonitorNews(url) { // rest of code return { ...newsContents }; } // rest of code

O simplemente cree un nuevo objeto sobre la marcha:

 async function getSpecificMonitorNews(url) { // rest of code return { title: $(".title-medium").text(), imageSrc: monitorBaseUrl + $(".lazy-img-container img").attr("src"), contents: $(".paragraph-wrapper > p").text(), date: $("time").text(), }; } // rest of code
about 4 years ago · Juan Pablo Isaza 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!