Bug in index.js MongooseStore.prototype.set
Opened this issue · 0 comments
In version 1.0.7
The set function uses the findOneAndUpdate and is expecting the new document to come back, but is missing the "new : true" flag. The catch statement receives the error as catch(e) but returns using the undefined err variable return(err) causing the original bug to be masked.
Original Code:
this.Session.findOneAndUpdate({ sid: sid }, s, { upsert: true }).exec(function (err, data) {
if (err) {
debug('SET error in DB call %s', err);
return fn(err);
}
debug('Session updated.');
var result;
try {
debug('Try to JSON parse %s', data.session);
result = JSON.parse(data.session);
}
catch(e) {
debug('Error after SET on JSON Parse %s', err);
return fn(err);
}
debug('Set cache and return the session %s', data.session);
this.cache.set(sid, result);
return fn(null, result);
}.bind(this));
Fixed Code:
this.Session.findOneAndUpdate({ sid: sid }, s, { new : true, upsert: true }).exec(function (err, data) { // SER added new flag
if (err) {
debug('SET error in DB call %s', err);
return fn(err);
}
debug('Session updated.');
var result;
try {
debug('Try to JSON parse %s', data.session);
result = JSON.parse(data.session);
}
catch(err) { /// SER changed e to err
debug('Error after SET on JSON Parse %s', err);
return fn(err);
}
debug('Set cache and return the session %s', data.session);
this.cache.set(sid, result);
return fn(null, result);
}.bind(this));
Stephen Rich