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); }) ); }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 codeO 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