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

340
Visualizações
How can i get the actual results from this code

When i console.log the output from getSpecificMonitorNews(url); i get the actual results. But when i use return await getSpecificMonitorNews(url);, the first results are overwritten by the last result received.

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 Respostas
Responde à pergunta

0

newsContents is a global variable so its value changes. You're returning a reference to that global object, not a copied version (and there's no need to copy it if you just bring it inside the function).

So you can either move newsContents inside the function if you don't need it elsewhere:

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

Or make a copy of the newsContents variable

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

Or just create a new object on the fly:

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 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