0

我正在尝试按名为“事件日期”的元字段对 Shopify 博客文章进行排序。我调用了我的articles.JSON,但它没有附带metafields.JSON,因此我必须获取该数组并将其放入foreach 函数中以将元字段附加到每篇文章。

这是每篇文章的 metafields.json 的设置方式:

{  
   "metafields":[  
      {  
         "id":5994805788772,
         "namespace":"global",
         "key":"Event-Date",
         "value":"1549256400",
         "value_type":"string",
         "description":null,
         "owner_id":15977611364,
         "created_at":"2019-02-06T18:31:44-05:00",
         "updated_at":"2019-02-06T18:31:44-05:00",
         "owner_resource":"article"
      },
      {  
         "id":5994805821540,
         "namespace":"global",
         "key":"Event-Time",
         "value":"6:00pm - 8:00pm",
         "value_type":"string",
         "description":null,
         "owner_id":15977611364,
         "created_at":"2019-02-06T18:31:45-05:00",
         "updated_at":"2019-02-06T18:31:45-05:00",
         "owner_resource":"article"
      },
      {  
         "id":6010564542564,
         "namespace":"global",
         "key":"Location",
         "value":"18th Street Location",
         "value_type":"string",
         "description":null,
         "owner_id":15977611364,
         "created_at":"2019-02-07T13:16:05-05:00",
         "updated_at":"2019-02-07T14:05:08-05:00",
         "owner_resource":"article"
      }
   ]
}

我如何在下面附加 metafields.JSON:

var request = new XMLHttpRequest();

request.open('GET', '/admin/blogs/43130421348/articles.json');
request.responseType = 'json';
request.send();

request.onload = function() {
    var articleList = request.response; 
    var articleArray = articleList.articles;
    var date = new Date();
    var ticks = Math.floor(date.getTime() / 1000);
    var count = 0;
    articleArray.forEach(function(entry,index, object){
    var metaRequest = new XMLHttpRequest();
    metaRequest.open('GET', '/admin/blogs/43130421348/articles/'+ entry.id + '/metafields.json');
    metaRequest.responseType = 'json';
    metaRequest.send();
    console.log(index);

    metaRequest.onload = function() {
        var articleMetaObj = metaRequest.response;
        var articleMetaArr = articleMetaObj.metafields;
        entry.metafields = articleMetaArr; 
        var eventDate = entry.metafields[0].value;
   }

 });

};

我现在正试图摆脱与当前日期相比已经过去的日期(“Key”:“Event-Date”)的任何文章。我查看了以下有关在 foreach 循环中删除对象的Stack Overflow 帖子,但没有一个解决方案证明实际上可以摆脱所有文章。它偶尔会摆脱所有这些,但有时会留在其中一个物体中。

我也尝试了一个数组过滤器,但是当我使用它时,我得到的只是一个空数组。我现在已经坚持了一段时间,因此非常感谢任何有关解决它的帮助。

4

2 回答 2

0

我认为如果您等到附加所有元数据,然后在完成后articleArray.filter取出您不想要的元数据,这将是最简单的。为此,您有两种选择:

选项 1 - 旧方法 (setInterval)

在这里,我们在检索元数据时保持计数,并创建一个间隔来检查它们何时全部完成。完成后,将调用一个函数(完成)以允许继续处理。

var request = new XMLHttpRequest();

request.open('GET', '/admin/blogs/43130421348/articles.json');
request.responseType = 'json';
request.send();

request.onload = function () {
    var articleList = request.response;
    var articleArray = articleList.articles;
    var date = new Date();
    var ticks = Math.floor(date.getTime() / 1000);
    var count = 0;  //to keep track of how many metafields have been retrieved
    var checkInterval = null;

    articleArray.forEach(function (entry, index) {
        var metaRequest = new XMLHttpRequest();
        metaRequest.open('GET', '/admin/blogs/43130421348/articles/' + entry.id + '/metafields.json');
        metaRequest.responseType = 'json';
        metaRequest.send();
        console.log(index);

        metaRequest.onload = function () {
            var articleMetaObj = metaRequest.response;
            var articleMetaArr = articleMetaObj.metafields;
            entry.metafields = articleMetaArr;
            count++;
        };
    });

    //Function to continue processing
    var finish = function () {
        articleArray = articleArray.filter(a => new Date(a.metafields[0].value).getTime() < date.getTime());
        //Continue on...
    };

    //Wait until all metafields are retrieved to continue
    checkInterval = setInterval(function () {
        if (count === articleArray.length - 1) {
            clearInterval(checkInterval);
            finish();
        }
    }, 500);
};

选项 2 - The New Razmatazz (Promises & async/await)

Promiseasync / await允许在处理异步操作时编写一些看起来更好看的代码。

如果您想使用这些,我建议您深入研究文档以更加熟悉,但这是您的任务可能的样子。

//Functions that return Promises can be awaited...
var get = url => new Promise((resolve, reject) => {
    var request = new XMLHttpRequest();
    request.open('GET', url);
    request.responseType = 'json';
    //resolve is called when successful
    request.onload = () => resolve(request.response);
    //reject is called when there's a problem
    request.onerror = err => reject(err);
    request.send();
});

//await keyword must appear in an async function
var getArticles = async () => {
    var articleList = await get('/admin/blogs/43130421348/articles.json');
    return articleList.articles;
};

//Promise.all takes an array of promises and resolves when all of them are finished
//This lets you skip the messy setInterval stuff
var getArticleMetafields = async articles => {
    var requests = [];
    articles.forEach(a => {
        var url = '/admin/blogs/43130421348/articles/' + a.id + '/metafields.json';
        var promise = get(url);
        requests.push(promise);
    });
    var responses = await Promise.all(requests);
    responses.forEach((response, i) => {
        articles[i].metafields = response.metafields;
    });
    return articles;
};

//Notice the async on the handler
document.addEventListener('DOMContentLoaded', async () => {
    var articles = await getArticles();
    articles = await getArticleMetafields(articles);
    var date = new Date();
    articles = articles.filter(a => new Date(a.metafields[0].value) < date);
    //Continue...
});

希望这可以帮助。干杯!

于 2019-02-12T20:20:08.630 回答
0

你的条目是文章吗?然后你可以忽略使用:

request.onload = function() {
var articleList = request.response; 
var articleArray = articleList.articles;
var date = new Date();
var ticks = Math.floor(date.getTime() / 1000);
var count = 0;
articleArray.forEach(function(entry,index, object){
   if(entry.'Key' !== 'Event-Date'){
        var metaRequest = new XMLHttpRequest();
        metaRequest.open('GET', '/admin/blogs/43130421348/articles/'+ entry.id + '/metafields.json');
        metaRequest.responseType = 'json';
        metaRequest.send();
        console.log(index);

        metaRequest.onload = function() {
        var articleMetaObj = metaRequest.response;
        var articleMetaArr = articleMetaObj.metafields;
        entry.metafields = articleMetaArr; 
        var eventDate = entry.metafields[0].value;
       }
   }
});
于 2019-02-12T20:10:06.797 回答