0

How can I refractor my code to get rid of this error from JSLinter? I tried moving the entire function out to a var but the code wasn't able to run after that.

  for (i = 0; i < timeDifference; i++) {
    timestamp ++;
    console.log(timestamp);
    energyDatum.find({timestamp: timestamp}).toArray(function(err, result) {
      var data = {};
      result.forEach(function(element) {
        data[element.deviceId] = element;
      });

      var roomRawData = [];
      mappings.forEach(function(room) {
        var hash = {};
        hash.floor = room.floor;
        hash.name = room.name;
        hash.room_type = room.room_type;
        hash.energy_ac = sumApplianceEnergy('energy_ac', room, data);
        hash.energy_light = sumApplianceEnergy('energy_light', room, data);
        hash.energy_socket_1 = sumApplianceEnergy('energy_socket_1', room, data);
        hash.energy_socket_2 = sumApplianceEnergy('energy_socket_2', room, data);
        hash.energy_socket_3 = sumApplianceEnergy('energy_socket_3', room, data);
        hash.energy_total = hash.energy_ac + hash.energy_light + hash.energy_socket_1 + hash.energy_socket_2 + hash.energy_socket_3;
        hash.timestamp = timestamp;
        roomRawData.push(hash);
      });

      roomRaw.insert(roomRawData, {w:1}, function(err, result) { console.log('done'); });

    });

    lastTimestamp.update({_id: timestampId}, {timestamp: timestamp});
  }
3
  • "the code wasn't able to run after that" isn't meaningful. Did you get any errors in the console? If you did, what were they? Commented Mar 27, 2015 at 11:42
  • You're closing over roomRawData in the inner function. You could get it out of the loop, but you'd have to pass roomRawData as an argument, rather than as a closure. I've never worked with JSLinter, so I'm not sure which functions are bothering it the most, though. Commented Mar 27, 2015 at 11:43
  • I think it doesn't like the function expression for the toArray callback. Just disable this rule, there's nothing wrong with your code. Commented Mar 27, 2015 at 11:49

1 Answer 1

1

JSLinter shows this message because your code has potential errors.

Take a look at this line:

energyDatum.find({timestamp: timestamp}).toArray(...);

This method is async, right? It means that the callback of toArray method is called after the for loop finishes its iterations, and therefore timestamp variable (when you use it inside this callback) doesn't have a value of current iteration, but instead it has value incremented for timeDifference times.

To solve this problem you could move this callback to another function:

var getIterationFunc = function(timestamp) {
    return function(err, result) {
        var data = {};
        // rest of function ...         
    }
}

and then use it:

energyDatum.find({timestamp: timestamp}).toArray(getIterationFunc(timestamp));

I believe this error should be fixed now. Hope this helps.

P.S. sorry for my English

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.