简体   繁体   English

如何多次运行javascript API?

[英]How do I run the javascript APIs multiple times?

I am trying to run a javascript API extraction multiple times to no avail. 我试图多次运行JavaScript API提取无济于事。 Here is what I have, but it returns the same values in the list I made. 这是我所拥有的,但是它在我创建的列表中返回相同的值。 I am trying to iterate between the pages of the API call. 我正在尝试在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();
}

You're resetting i in your interior for loop. 您正在将i重置for内部循环。 Just use another iterator there: 在那里使用另一个迭代器:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM