So this is most likely simple but I have spent all day trying different approaches and googling for solutions.
for some reason when I use + with 2 numbers instead of adding the numbers together node is joining the numbers.
Company.findById(req.id, (err, doc) => {
if (err) {
callback(err, err);
} else {
var i = doc.TotalGranted; // i.e number value of 200
var e = doc.TotalAvailable; // i.e number value of 200
var newTotalgranted = i + req.newNumber; // i.e number value of 100
var newAvailable = e - req.newNumber; // i.e number value of 100
doc.TotalGranted= newTotalgranted;
doc.TotalAvailable= newAvailable;
doc.save((err, doc) => {
if (err) {
callback(err, err);
}
callback(null, 'success');
});
}
});
now instead of totalgranted = 300 instead it equals 200,100
if it helps here is the relevant section of the returned document from mongo, these are not contained in an array
TotalAvailable: 200,
TotalGranted: 200,
newNumber: '100',
the new number is a string but its actually coming from a returned document as well. so maybe i need to convert the newNumber from a string i have tried this but can not work it out.
req.newNumberusage withNumber(req.newNumber)i + parseInt(req.newNumber)?newNumber: '100'. AndsomeString + whateverorwhatever + someStringperforms a string concatenation. That's the expected behaviour, not a bug.