使用从Xray收集的元数据填充数组

时间:2022-01-20 20:34:57

I've been trying to fill an array with metadata that I collect with Xray, and haven't had any success. The function is called by an API route on my server and gets the links from my application.

我一直在尝试使用Xray收集的元数据填充数组,并且没有取得任何成功。该函数由我的服务器上的API路由调用,并从我的应用程序获取链接。

I seem to be struggling with promises as it takes time to scrape the metadata, and I can't seem to get the function to wait until the data has been collected before moving on. Perhaps, I'm just not understanding how Xray works? Or maybe promises? I've tried everything I can think of, this being the most recent attempt (and the simplest):

我似乎正在努力应对承诺,因为它需要时间来刮取元数据,而且我似乎无法让函数等到数据被收集后再继续。也许,我只是不明白Xray是如何工作的?或者也许是承诺?我已经尝试了我能想到的一切,这是最近的尝试(也是最简单的):

  function createCollection() {
    Promise.all(rawLinks.map(function(link) {
      linksArray.push(xray(link, 'title')(function(error, title) {
        console.log(title);
        return title;
      }))
    }))
    .then(linksArray => {
      console.log(linksArray);
    });
  }

It's by far not the most robust or elaborate solution I've tried, but it's the most recent one. First the console logs an array with "undefined" as the data, THEN it logs the individual titles.

到目前为止,它并不是我尝试过的最强大或最精细的解决方案,但它是最新的解决方案。首先,控制台记录一个带有“undefined”的数组作为数据,然后记录各个标题。

I would be very thankful for any help, or direction on what to research. Like I've said, I feel as if I've exhausted all my ideas and don't know where to even look anymore.

我非常感谢任何帮助或研究方向。就像我说的那样,我觉得我已经筋疲力尽了所有的想法,不知道哪里再看了。

1 个解决方案

#1


0  

Figured it out, this seems to be doing the trick!

弄清楚,这似乎是诀窍!

  // format links into an array of objects
  var rawLinks = links.split(', ');
  var linksArray = [];

  createCollection();

  function createCollection() {
    rawLinks.map(function(link) {
      var fillMetaPromise = new Promise(
        function(resolve, reject) {
          var test = xray(link, 'title')(function(err, title) {
            var data = { title: title, link: link };
            resolve(data);
          });
        })
        .then(data => {
          processTitle(data.title, data.link);
        });
    });
  }

  function processTitle(title, link) {
    var object = {
      link: link,
      title: title
    };

    linksArray.push(object);
    console.log(linksArray);
  }

#1


0  

Figured it out, this seems to be doing the trick!

弄清楚,这似乎是诀窍!

  // format links into an array of objects
  var rawLinks = links.split(', ');
  var linksArray = [];

  createCollection();

  function createCollection() {
    rawLinks.map(function(link) {
      var fillMetaPromise = new Promise(
        function(resolve, reject) {
          var test = xray(link, 'title')(function(err, title) {
            var data = { title: title, link: link };
            resolve(data);
          });
        })
        .then(data => {
          processTitle(data.title, data.link);
        });
    });
  }

  function processTitle(title, link) {
    var object = {
      link: link,
      title: title
    };

    linksArray.push(object);
    console.log(linksArray);
  }