0

I'm just getting back into Node.js after not looking at it for about 2 years. I'm wondering what the best way to handle the callbacks / "flow" of this program. Currently it rewrites the same file over and over. The filename doesn't stay synced to the data being downloaded. What is the best way to handle this?

It writes the last date as the filename - "2017-11-10.json". Specifically what mechanism do I use to get the correct d variable passed into the https.get. It doesn't allow extra variables to be passed. So what is the proper mechanism or scoping to achieve separate files for each response.

    function dstr(date) {
      let prep = (date.getMonth() + 1) + "/" + (date.getDay() + 1) + "/" + date.getFullYear();
      let r = encodeURIComponent(prep);
      return r;
    }

    var https = require('https');
    var fs = require('fs');

    let d = new Date("2017-11-15"); 

    var url = "https://somewebsite.com/weather_stats/_Search?date={date}&last24=false";

    let dates = [];
    for (var i = 0; i < 5; i++) {
      d.setDate(d.getDate() - 1);
      dates.push(d);
    }

    dates.forEach( (x) => {
        const u = url.replace("{date}", dstr(x));

        https.get(u, (resp) => {
          const filename = 'C:\\data\\' + x.toISOString().split('T')[0] + '.json'
          let data = '';

          // A chunk of data has been recieved.
          resp.on('data', (chunk) => {
            data += chunk;
          });

          // The whole response has been received. Print out the result.
          resp.on('end', () => {
            console.log(JSON.parse(data).length);
            fs.writeFile(filename, data, function(err) {
            if(err) {
                return console.log(err);
            }
                console.log("The file was saved!");
            }); 
          });

        }).on("error", (err) => {
          console.log("Error: " + err.message);
        });
    });
BuddyJoe
  • 69,735
  • 114
  • 291
  • 466
  • @SLaks I'm editing the question I did not find the exact question. But if you can point me to one please let me know. – BuddyJoe Nov 16 '17 at 17:08
  • That duplicate is exactly your problem; all of your callbacks share the same variable. – SLaks Nov 16 '17 at 17:09
  • Which variable? where does the problem stem from? Sorry. I know I'm having a dense moment. Should I put all possible dates into an array and feed that into the callbacks. – BuddyJoe Nov 16 '17 at 17:15
  • The `d` variable. You need a separate `const` inside the loop body. – SLaks Nov 16 '17 at 17:24
  • so would that just look like: const cd = d; – BuddyJoe Nov 16 '17 at 20:13
  • or is it better to put all the dates in an array and call .each() on it? Does that get around this too. – BuddyJoe Nov 16 '17 at 20:14
  • Modifiying the source one more time @SLaks. Guess I'm still missing what is still causing the issue. Is this a JavaScript thing. Don't think I have had this issue with lambda/closures in C#. – BuddyJoe Nov 16 '17 at 21:09
  • 1
    Your array has the same mutable `Date` instances every time. You need to make new instances. – SLaks Nov 16 '17 at 23:18
  • Thanks. that fixed it. Appreciate your time and patience. – BuddyJoe Nov 18 '17 at 00:35

0 Answers0