链接请求并映射每个对象

时间:2019-11-19 21:43:06

标签: node.js json express axios

我试图基于返回json的主get请求执行多个get请求。更具体地说,我正在使用express.js和axios以及pushshift API来获取Sumbissions及其注释。

主要json如下:

{
    "data": [
        {
            "author": "[deleted]",
            "author_flair_css_class": null,
            "author_flair_text": null,
            "banned_by": "moderators",
            "brand_safe": true,
            "can_mod_post": false,
            "contest_mode": false,
            "created_utc": 1514766734,
            "domain": "self.selfimprovement",
            "full_link": "https://www.reddit.com/r/selfimprovement/comments/7nc42d/is_theres_any_value_in_retainingreviewing_the/",
            "id": "7nc42d",
            "is_crosspostable": false,
            "is_reddit_media_domain": false,
            "is_self": true,
            "is_video": false,
            "locked": false,
            "num_comments": 1,
            "num_crossposts": 0,
            "over_18": false,
            "parent_whitelist_status": "all_ads",
            "permalink": "/r/selfimprovement/comments/7nc42d/is_theres_any_value_in_retainingreviewing_the/",
            "pinned": false,
            "retrieved_on": 1514841211,
            "score": 1,
            "spoiler": false,
            "stickied": false,
            "subreddit": "selfimprovement",
            "subreddit_id": "t5_2qmbm",
            "subreddit_type": "public",
            "thumbnail": "default",
            "title": "Is there's any value in retaining/reviewing the past (through videos, photos, writing, etc) beyond nostalgia?",
            "url": "https://www.reddit.com/r/selfimprovement/comments/7nc42d/is_theres_any_value_in_retainingreviewing_the/",
            "whitelist_status": "all_ads"
        },
        { ....
        }
      ]
}

我必须获取所有提交的内容,然后将每个提交的ID传递给多个请求,以便我可以获取每个提交的评论。我的代码当前如下所示:

function getSubmissions() {
    axios.get('https://api.pushshift.io/reddit/search/submission/?&after=1514764800&size=10&subreddit=selfimprovement')
        .then(function(response) {

            const promises = response.data.map(comment => axios.get('https://api.pushshift.io/reddit/comment/search/?link_id='+comment.id).then(({data})=>data));

              return Promise.all(promises)
             .then(values => {
                 res.json(values);
             });
         })
         .catch(function (error) {

            console.log('Error: ' + error);
        });
    }
}

此代码的结果是res未定义。

0 个答案:

没有答案