diff --git a/spec/RestQuery.spec.js b/spec/RestQuery.spec.js index 9e2b775922..2aa5add447 100644 --- a/spec/RestQuery.spec.js +++ b/spec/RestQuery.spec.js @@ -235,4 +235,36 @@ describe('rest query', () => { done(); }); }); + + it('makes sure null pointers are handed correctly #2189', done => { + let object = new Parse.Object('AnObject'); + let anotherObject = new Parse.Object('AnotherObject'); + anotherObject.save().then(() => { + object.set('values', [null, null, anotherObject]); + return object.save(); + }).then(() => { + let query = new Parse.Query('AnObject'); + query.include('values'); + return query.first(); + }).then((result) => { + let values = result.get('values'); + expect(values.length).toBe(3); + let anotherObjectFound = false; + let nullCounts = 0; + for(let value of values) { + if (value === null) { + nullCounts++; + } else if (value instanceof Parse.Object) { + anotherObjectFound = true; + } + } + expect(nullCounts).toBe(2); + expect(anotherObjectFound).toBeTruthy(); + done(); + }, (err) => { + console.error(err); + fail(err); + done(); + }); + }); }); diff --git a/src/RestQuery.js b/src/RestQuery.js index 0dc95ff341..6752436c54 100644 --- a/src/RestQuery.js +++ b/src/RestQuery.js @@ -480,6 +480,9 @@ function includePath(config, auth, response, path) { let pointersHash = {}; var objectIds = {}; for (var pointer of pointers) { + if (!pointer) { + continue; + } let className = pointer.className; // only include the good pointers if (className) { @@ -542,7 +545,7 @@ function findPointers(object, path) { } if (path.length == 0) { - if (object.__type == 'Pointer') { + if (object === null || object.__type == 'Pointer') { return [object]; } return []; @@ -564,7 +567,7 @@ function findPointers(object, path) { function replacePointers(object, path, replace) { if (object instanceof Array) { return object.map((obj) => replacePointers(obj, path, replace)) - .filter((obj) => obj != null && obj != undefined); + .filter((obj) => typeof obj !== 'undefined'); } if (typeof object !== 'object') { @@ -572,7 +575,7 @@ function replacePointers(object, path, replace) { } if (path.length === 0) { - if (object.__type === 'Pointer') { + if (object && object.__type === 'Pointer') { return replace[object.objectId]; } return object;