Axios ResponseRequest博客帖子

时间:2017-12-23 04:03:23

标签: node.js axios

我希望使用Axios来向Blog API发出GET请求,转换响应,然后发出POST请求,以便将转换后的数据发送回API。这样做的原因是从我的博客中删除了额外的Wordpress标记。我能够向单个博客帖子发出请求转换响应,然后发出帖子请求,这正确地提交了在“post_body”中返回的数据。现在我正在尝试为我的所有博客文章执行此操作,但由于数据结构现在已更改所有博客帖子都在对象数组中,因此我想使用forEach以循环对象数组并返回元素。我一直得到一个不确定的回应。有没有人有任何建议。

axios.get(allPosts, {
    transformResponse: axios.defaults.transformResponse.concat(function(data, headers) {
        // use data I passed into the function and the objects from the API
        // pass in data into the function using forEach this will return an array
        data.objects.forEach(function(data) {
            // use the returned array on Objects.key to find the name of the array
            Object.keys(data).forEach(function(k) {
                // if the key equals execute code
                if (k ==  "post_body") {
                    // fire Regex
                    data[k] = i[k].replace(regExp, '');
                }
            })
        })
        // return data
        return data.objects;
    })
})
.then(function(res) {
    console.log(res.data.post_body);
})

//来自API的示例数据

{
"limit": 20,
"objects": [
    {
        "allow_comments": true,
        "post_body":"<p>Sample Blog Post</p>",
        "use_featured_image_in_summary": true,
        "uses_default_template": false,
        "weekly_notification_email_id": "5489696311"
    }
],
"offset": 0,
"total": 5,
"total_count": 5

}

0 个答案:

没有答案