如何多次运行javascript API?

时间:2017-06-30 05:20:25

标签: javascript json api

我试图多次运行javascript API提取无济于事。这是我所拥有的,但它在我所做的列表中返回相同的值。我试图在API调用的页面之间进行迭代。

const https = require('https');
ids = []

for (var i = 1; i < 3; i++) {
   options = {
       hostname: 'api.intercom.io',
       port: 443,
       path: '/conversations?per_page=10page=' + i + "",
       method: 'GET',
       headers: {
           "Accept": "application/json",
           "Authorization": "XXXX"
       } // add headers here

   };

   req = https.request(options, (res) => {
       console.log('statusCode:', res.statusCode);
       console.log('headers:', res.headers);

       res.on('data', (data) => {
           var d = JSON.parse(data);
           len = d.conversations.length
           for (i = 0; i < len; i++) {
               ids.push(d.conversations[i].id)
           }
           console.log(ids)
       });
   });

   req.on('error', (e) => {
       console.error(e);
   });

   req.end();
}

1 个答案:

答案 0 :(得分:0)

您在内部i循环中重置for。只需在那里使用另一个迭代器:

for (var j = 0; j < len; j++) {
   ids.push(d.conversations[i].id)
}