mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-16 10:46:14 +01:00
closes #2628
This commit is contained in:
@@ -174,6 +174,31 @@ module.exports = function(db, module) {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
module.isObjectFields = function(key, fields, callback) {
|
||||||
|
if (!key) {
|
||||||
|
return callback();
|
||||||
|
}
|
||||||
|
|
||||||
|
var data = {};
|
||||||
|
fields.forEach(function(field) {
|
||||||
|
field = helpers.fieldToString(field);
|
||||||
|
data[field] = '';
|
||||||
|
});
|
||||||
|
|
||||||
|
db.collection('objects').findOne({_key: key}, {fields: data}, function(err, item) {
|
||||||
|
if (err) {
|
||||||
|
return callback(err);
|
||||||
|
}
|
||||||
|
var results = [];
|
||||||
|
|
||||||
|
fields.forEach(function(field, index) {
|
||||||
|
results[index] = !!item && item[field] !== undefined && item[field] !== null;
|
||||||
|
});
|
||||||
|
|
||||||
|
callback(null, results);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
module.deleteObjectField = function(key, field, callback) {
|
module.deleteObjectField = function(key, field, callback) {
|
||||||
callback = callback || helpers.noop;
|
callback = callback || helpers.noop;
|
||||||
if (!key || !field) {
|
if (!key || !field) {
|
||||||
|
|||||||
@@ -222,7 +222,7 @@ describe('Hash methods', function() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('should return false if field does not exist', function(done) {
|
it('should return false if field does not exist', function(done) {
|
||||||
db.isObjectField('testObject1', 'field1', function(err, value) {
|
db.isObjectField('hashTestObject', 'field1', function(err, value) {
|
||||||
assert.equal(err, null);
|
assert.equal(err, null);
|
||||||
assert.equal(arguments.length, 2);
|
assert.equal(arguments.length, 2);
|
||||||
assert.equal(value, false);
|
assert.equal(value, false);
|
||||||
@@ -240,6 +240,27 @@ describe('Hash methods', function() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
describe('isObjectFields()', function() {
|
||||||
|
it('should return an array of false if object does not exist', function(done) {
|
||||||
|
db.isObjectFields('doesnotexist', ['field1', 'field2'], function(err, values) {
|
||||||
|
assert.equal(err, null);
|
||||||
|
assert.equal(arguments.length, 2);
|
||||||
|
assert.deepEqual(values, [false, false]);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return false if field does not exist', function(done) {
|
||||||
|
db.isObjectFields('hashTestObject', ['name', 'age', 'field1'], function(err, values) {
|
||||||
|
assert.equal(err, null);
|
||||||
|
assert.equal(arguments.length, 2);
|
||||||
|
assert.deepEqual(values, [true, true, false]);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe('deleteObjectField()', function() {
|
describe('deleteObjectField()', function() {
|
||||||
before(function(done) {
|
before(function(done) {
|
||||||
db.setObject('testObject10', {foo: 'bar', delete: 'this'}, done);
|
db.setObject('testObject10', {foo: 'bar', delete: 'this'}, done);
|
||||||
|
|||||||
Reference in New Issue
Block a user