在节点错误中遍历axios request.data [英] Loop through axios request.data in node error

查看:75
本文介绍了在节点错误中遍历axios request.data的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试遍历axios请求,但是我一直在日志中收到错误,我正在遍历我的数据,返回的数据结构看起来像这样

i have been trying to loop through an axios request but i keep getting the error in my logs, i am trying to loop through my data, the returned structure of my data looks like this

{2 items
"data":[1 item
0:{...}34 items
]
"pagination":{4 items
"page":0
"itemsPerPage":0
"hasNextPage":true
"hasPrevPage":true
}
}

这是错误

scheduledFunction
SyntaxError: Unexpected token o in JSON at position 1 at JSON.parse (<anonymous>)

这是我的代码

var options = {
  method: 'GET',
  url: 'https://elenasport-io1.p.rapidapi.com/v2/fixtures',
  params: {to: d3, page: '1', from: d2},
  headers: {
    'x-rapidapi-key': '63836fd5cemshe5de364e7512350p145aebjsn744427db1224',
    'x-rapidapi-host': 'elenasport-io1.p.rapidapi.com'
  }
};

axios.request(options).then(function (response) {



    //console.log(response.data);


    data = JSON.parse(response.data); // this is the data you need to process in the request

    console.log(data);

    data.pagination.forEach(obj => {
        Object.entries(obj).forEach(([key, value]) => {
            console.log(`${key} ${value}`);
        });
        





}).catch(function (error) {
    console.error(error);
});





})

推荐答案

如果数据不是JSON,则只需将其解析为JSON.在这里,您已经以JSON的形式获取了响应数据,无需再次解析它.

You only need to parse data to JSON if it is not coming as the JSON. Here, you are already getting the response data as JSON, you don't need to parse it again.

axios.request(options).then(function (response) {

    console.log(typeof response); // check the type of response returning already in JSON format
    console.log(response); // check the response object and based on key/values process it 

    const data = response.data; // if resp contains the data you will get it here.

    console.log(data);

    data.pagination.forEach(obj => {
        Object.entries(obj).forEach(([key, value]) => {
            console.log(`${key} ${value}`);
        });  

}).catch(function (error) {
    console.error(error);
});

})

注意:Axios根据HTTP响应的Content-Type标头解析响应.当响应的内容类型为application/json时,Axios将自动尝试将响应解析为JavaScript对象.有关更多详细信息,请检查.

Note : Axios parses the response based on the HTTP response's Content-Type header. When the response's content type is application/json , Axios will automatically try to parse the response into a JavaScript object. For more details check this.

此外,分页是一个对象,它不可迭代,而是用于for/in

Also, pagination is an object, which is not iterable instead use for/in

         
axios.request(options).then(function (response) {

    console.log(typeof response); // check the type of response returning already in JSON format
    console.log(response); // check the response object and based on key/values process it 

    const data = response.data; // if resp contains the data you will get it here.

    console.log(data);

  for (let data1 in data.pagination) {
    // from the sample response you shared in the question 
    console.log(data1) // prints the keys
    console.log(data.pagination.data1) // prints the values

}

}).catch(function (error) {
    console.error(error);
});

})

       

这篇关于在节点错误中遍历axios request.data的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆