mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-16 10:46:14 +01:00
fix db.incrObjectFieldBy for mongo
convert string to int
This commit is contained in:
@@ -231,9 +231,11 @@ module.exports = function(db, module) {
|
|||||||
|
|
||||||
module.incrObjectFieldBy = function(key, field, value, callback) {
|
module.incrObjectFieldBy = function(key, field, value, callback) {
|
||||||
callback = callback || helpers.noop;
|
callback = callback || helpers.noop;
|
||||||
if (!key) {
|
value = parseInt(value, 10);
|
||||||
|
if (!key || isNaN(value)) {
|
||||||
return callback();
|
return callback();
|
||||||
}
|
}
|
||||||
|
|
||||||
var data = {};
|
var data = {};
|
||||||
field = helpers.fieldToString(field);
|
field = helpers.fieldToString(field);
|
||||||
data[field] = value;
|
data[field] = value;
|
||||||
|
|||||||
@@ -347,7 +347,7 @@ describe('Hash methods', function() {
|
|||||||
|
|
||||||
it('should set an objects field to 5 if object does not exist', function(done) {
|
it('should set an objects field to 5 if object does not exist', function(done) {
|
||||||
db.incrObjectFieldBy('testObject16', 'field1', 5, function(err, newValue) {
|
db.incrObjectFieldBy('testObject16', 'field1', 5, function(err, newValue) {
|
||||||
assert.equal(err, null);
|
assert.ifError(err);
|
||||||
assert.equal(arguments.length, 2);
|
assert.equal(arguments.length, 2);
|
||||||
assert.equal(newValue, 5);
|
assert.equal(newValue, 5);
|
||||||
done();
|
done();
|
||||||
@@ -356,12 +356,20 @@ describe('Hash methods', function() {
|
|||||||
|
|
||||||
it('should increment an object fields by passed in value and return it', function(done) {
|
it('should increment an object fields by passed in value and return it', function(done) {
|
||||||
db.incrObjectFieldBy('testObject15', 'age', 11, function(err, newValue) {
|
db.incrObjectFieldBy('testObject15', 'age', 11, function(err, newValue) {
|
||||||
assert.equal(err, null);
|
assert.ifError(err);
|
||||||
assert.equal(arguments.length, 2);
|
assert.equal(arguments.length, 2);
|
||||||
assert.equal(newValue, 111);
|
assert.equal(newValue, 111);
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should increment an object fields by passed in value and return it', function(done) {
|
||||||
|
db.incrObjectFieldBy('testObject15', 'age', '11', function(err, newValue) {
|
||||||
|
assert.ifError(err);
|
||||||
|
assert.equal(newValue, 122);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user