0

I am running this code, which should compare saved webpages with the current live versions, but it seems to only do so for the last entry:

ipcMain.on('compare', (event, arg) => {
  index.find({
    "type": "website"
  }, function(err, docs) {
    var doc;
    var data;
    for (i = 0; i < docs.length; i++) {
      console.log(doc = docs[i]);
      console.log(i);
      request('GET', doc.URL).done(function(res) {
        data = fs.readFileSync(DataPath + "/savedpages/" + doc.Name + ".html");
        console.log(data);
        console.log(res.getBody())
        if (data == res.getBody()) {
          index.update({
            _id: doc.ID
          }, {
            $set: {
              Changes: false
            }
          }, function(err, updateval) {
            if (err) throw err;
            console.log(doc.Name);
            event.sender.send('update-false', doc.Name + "-changescell")
          })
        } else {
          index.update({
            _id: doc.ID
          }, {
            $set: {
              Changes: true
            }
          }, function(err, updateval) {
            if (err) throw err;
            console.log(doc.Name);
            event.sender.send('update-true', doc.Name + "-changescell")
          })
        }
      })
    }
  })
})

The output looks like this:

{ Changes: false,
type: 'website',
Date: 2017-07-30T14:04:35.592Z,
Name: 'petra',
URL: 'http://petra.oldisoft.de',
_id: 'SXtHgqAWniDBAibJ' }
0
{ Changes: false,
type: 'website',
Date: 2017-07-30T14:03:26.658Z,
Name: 'heise',
URL: 'http://heise.de',
_id: 'zOc1Wnm801huVPjs' }
1
<Buffer 3c 21 44 4f 43 54 59 50 45 20 68 74 6d 6c 3e 0a 3c 68 74 6d 6c 20 6c 
61 6e 67 3d 22 64 65 22 3e 0a 0a 3c 68 65 61 64 3e 0a 20 20 20 20 3c 74 69 
74 6c ... >
<Buffer 3c 2f 68 65 61 64 3e 0a 3c 62 6f 64 79 20 6f 6e 6c 6f 61 64 3d 27 6a 
61 76 61 73 63 72 69 70 74 3a 64 6f 63 75 6d 65 6e 74 2e 61 6e 6d 65 6c 64 
75 6e ... >
heise
<Buffer 3c 21 44 4f 43 54 59 50 45 20 68 74 6d 6c 3e 0a 3c 68 74 6d 6c 20 6c 
61 6e 67 3d 22 64 65 22 3e 0a 0a 3c 68 65 61 64 3e 0a 20 20 20 20 3c 74 69 
74 6c ... >
<Buffer 3c 21 44 4f 43 54 59 50 45 20 68 74 6d 6c 3e 0a 3c 68 74 6d 6c 20 6c 
61 6e 67 3d 22 64 65 22 3e 0a 0a 3c 68 65 61 64 3e 0a 20 20 20 20 3c 74 69 
74 6c ... >
heise

It seems to me that the code skips a part of the code while the for loop is running. I don't understand why it is behaving as such.

Nisarg Shah
  • 14,151
  • 6
  • 34
  • 55
Shura
  • 3
  • 1
  • Everything inside of `done` is async code, so it won't run synchronously with the rest of the code. You really should have tried to reduce this down to a [MCVE] first though. This is a lot of sense code to dig through. – Carcigenicate Jul 30 '17 at 14:23

1 Answers1

0

this is because you are running async code in a loop.

To overcome this you need to create a closure by wrapping the async code in a function:

for(i = 0; i < docs.length; i++){
  console.log(doc = docs[i]);
  console.log(i);
    runAsyncCode(doc)
  }

function runAsyncCode(doc){
    request('GET', doc.URL).done(function(res) {
          data = fs.readFileSync(DataPath + "/savedpages/" + doc.Name + ".html");
          console.log(data);
          console.log(res.getBody())
          if(data == res.getBody()){
            index.update({_id: doc.ID}, {$set: {Changes:false}}, function(err, updateval){
              if(err) throw err;
              console.log(doc.Name);
              event.sender.send('update-false', doc.Name + "-changescell")
            })
          } else {
            index.update({_id: doc.ID}, {$set: {Changes:true}}, function(err, updateval){
              if(err) throw err;
              console.log(doc.Name);
              event.sender.send('update-true', doc.Name + "-changescell")
            })
          }
        })
}
Tomer
  • 17,787
  • 15
  • 78
  • 137