lundi 9 mars 2015

Sails js: service variable initialization

The code below is just for reference. Assume my service name is incremented and it has the incrementCounter function



var counter = 0;
module.exports = {
incrementCounter: function (req, callback) {
counter++;
callback(null, counter);
}
}


As you can see var counter = 0 is defined outside the function so it is globally accessible inside all service functions. Now if i call the service twice in parallel it gives 1 for the first call which is correct but for the second call it is giving me 2. Shouldn't the variable counter should reinitialize itself with 0 when ever the service will be called?



incremented.incrementCounter(req, function(error, response){
//response = 1
});

incremented.incrementCounter(req, function(error, response){
//response = 2
});

Aucun commentaire:

Enregistrer un commentaire