Using a filter with exclusion of a non existent property, removes an existing one (#1257)

* #fixes 1256

* Added missing check for returned error

* Fixed test to check proper fields in each doc
This commit is contained in:
Dimitris 2017-03-21 01:18:48 +02:00 committed by Sakib Hasan
parent 3c7e398f6f
commit 2bfc769c4d
3 changed files with 23 additions and 1 deletions

View File

@ -265,7 +265,7 @@ function fieldsToArray(fields, properties, excludeUnknown) {
} else if (excluded.length > 0) {
for (i = 0, n = excluded.length; i < n; i++) {
var index = result.indexOf(excluded[i]);
result.splice(index, 1);
if (index !== -1) result.splice(index, 1); // only when existing field excluded
}
}
}

View File

@ -599,6 +599,27 @@ describe('basic-querying', function() {
sample(['email']).expect(['email']);
});
it('should ignore non existing properties when excluding', function(done) {
return User.find({fields: {notExist: false}}, (err, users) => {
if (err) return done(err);
users.forEach(user => {
switch (user.seq) { // all fields depending on each document
case 0:
case 1:
Object.keys(user.__data).should.containDeep(['id', 'seq', 'name', 'order', 'role',
'birthday', 'vip', 'address', 'friends']);
break;
case 4: // seq 4
Object.keys(user.__data).should.containDeep(['id', 'seq', 'name', 'order']);
break;
default: // Other records, seq 2, 3, 5
Object.keys(user.__data).should.containDeep(['id', 'seq', 'name', 'order', 'vip']);
}
});
done();
});
});
var describeWhenNestedSupported = connectorCapabilities.nestedProperty ? describe : describe.skip;
describeWhenNestedSupported('query with nested property', function() {
it('should support nested property in query', function(done) {

View File

@ -46,6 +46,7 @@ describe('util.fieldsToArray', function() {
sample({'bat': true, unknown: true}, true).expect(['bat']);
sample({'bat': 0}, true).expect(['foo', 'bar', 'baz']);
sample({'bat': false}, true).expect(['foo', 'bar', 'baz']);
sample({'other': false}, true).expect(['foo', 'bar', 'bat', 'baz']);
});
});