我有一个包含对象的数组,我希望遍历这些对象以执行AXIOS调用并使用函数操作响应.不幸的是,最终输出是一个数组,其中包含多个具有相同重复对象的嵌套数组,该数组只有数组报纸的第一个元素的结果.

const newspapers= [{
    "name": "CNN",
    "address": "https://edition.cnn.com/specials/world/cnn-climate",
    "base": "https://edition.cnn.com"
  },
  {
    "name": "The Guardian",
    "address": "https://www.theguardian.com/environment/climate-crisis",
    "base": "https://www.theguardian.com"
  }, etc...]

// Initiate global variable for the results
let articles = [];

// Function to remove duplicates, get img if present and consolidate data
function storeData(element, base, name) {
  const results = [];
  element.find("style").remove();
  const title = element.text();
  const urlRaw = element.attr("href");
  const url =
    urlRaw.includes("www") || urlRaw.includes("http") ? urlRaw : base + urlRaw;

  // Check for duplicated url
  if (tempUrls.indexOf(url) === -1) {
    // Check for social media links and skip
    if (!exceptions.some((el) => url.toLowerCase().includes(el))) {
      tempUrls.push(url);

      // Get img if child of anchor tag
      const imageElement = element.find("img");
      if (imageElement.length > 0) {
        // Get the src attribute of the image element

        results.push({
          title,
          url,
          source: name,
          imgUrl: getImageFromElement(imageElement),
        });
      } else {
        results.push({
          title,
          url: url,
          source: name,
        });
      }
    }
  }
  return results;
}

// Cheerio function
function getElementsCheerio(html, base, name, searchterms) {
  const $ = cheerio.load(html);

  const termsAlso = searchterms.also;
  const termsOnly = searchterms.only;
  const concatInfo = [];

  termsAlso.forEach((term) => {
    $(`a:contains("climate"):contains(${term})`).each(function () {
      const tempData = storeData($(this), base, name);
      tempData.map((el) => concatInfo.push(el));
    });
  });

  termsOnly.forEach((term) => {
    $(`a:contains(${term})`).each(function () {
      const tempData = storeData($(this), base, name);
      tempData.map((el) => concatInfo.push(el));
    });
  });
  return concatInfo;
}

// API
app.get("/news", (req, res) => {
  // Query String
  const query = checkForQuery(req);
  const wordsToSearch = query ? verifyQuery(query) : "";

  Promise.all(
    newspapers.map(({ name, address, base }) =>
      axios
        .get(address, {
          headers: { "Accept-Encoding": "gzip,deflate,compress" },
        })
        .then((res) => {
          const html = res.data;
          console.log({ name, address, base });

          const scrappedElements = getElementsCheerio(
            html,
            base,
            name,
            wordsToSearch
          );
          scrappedElements.map((item) => articles.push(item));

          return articles;
        })
    )
  ).then((articles) => {
    res.json(articles);
  });
});


当我记录循环时,我看到它正在正确地通过,但是从第一份报纸检索到的相同的两篇文章也出现在所有其他报纸上:

console.log / result:
{
  name: 'CNN',
  address: 'https://edition.cnn.com/specials/world/cnn-climate',
  base: 'https://edition.cnn.com'
}
[{title: article1,
url: article1,
source: article1,
imgUrl: article1}, 
{title: article2,
url: article2,
source: article2,
imgUrl: article2}]
{
  name: 'The Times',
  address: 'https://www.thetimes.co.uk/environment/climate-change',
  base: 'https://www.thetimes.co.uk'
}
[{title: article1,
url: article1,
source: article1,
imgUrl: article1}, 
{title: article2,
url: article2,
source: article2,
imgUrl: article2}]
etc...

我怎么才能解决这个问题呢?为什么即使包含另一家报纸信息的新对象正在经过,它总是从第一个开始收集相同的文章?

我们对任何帮助都深表感谢.我是一名前端开发人员,这么做是为了学习,我知道我可能缺乏一些基本知识来避免这个愚蠢的问题.提前谢谢您!

推荐答案

你不需要articles

更改这一点:

scrappedElements.map((item) => articles.push(item));

return articles;

对此

return scrappedElements

Node.js相关问答推荐

如何使用Express正确跟踪服务器应用程序上的所有传出的Node.js请求

node 模块错误:类型参数OT具有循环约束''

如何在Reaction应用程序中查看存储的斑点图像?

在编译时强制不缩小类型范围

EJS ForEach循环标记

FHIR 服务器:尽管 JSON 格式正确,但在 POST 请求中接收未定义请求正文

如何创建具有不同对象类型的数组类型

是否可以在 NodeJS 代码库中的每个函数之前和之后添加 console.log?

与诗乃一起嘲笑奈克斯

我应该如何解决这个 Angular node 包模块依赖冲突?

Nodejs mongoose 在一个查询中从多个集合中获取结果

在 Passport 策略回调中获取请求对象

Node.js mongodb 驱动程序异步/等待查询

使用 node.js 执行一个 exe 文件

从 CoffeeScript 中的数组中删除一个值

Node.js + Express:应用程序不会开始监听端口 80

为什么 Node 控制台不显示功能代码?

续集findbyid不是一个函数,但显然findAll是

NodeJS 中的 HTTPS 请求

Mongoose - 验证邮箱语法