Skip to content

fix: Parse.Query.findAll not returning all objects with option json: true #2449

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions integration/test/ParseQueryTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -1493,13 +1493,17 @@ describe('Parse Query', () => {

it('can return all objects with findAll', async () => {
const objs = [...Array(101)].map(() => new Parse.Object('Container'));

await Parse.Object.saveAll(objs);

const query = new Parse.Query('Container');

const result = await query.findAll();
assert.equal(result.length, 101);
});

it('can return all objects with findAll json option', async () => {
const objs = [...Array(101)].map(() => new Parse.Object('Container'));
await Parse.Object.saveAll(objs);
const query = new Parse.Query('Container');
const result = await query.findAll({ json: true, batchSize: 100 });
assert.equal(result.length, 101);
});

Expand Down
35 changes: 8 additions & 27 deletions src/ParseQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -721,6 +721,7 @@ class ParseQuery {
* be used for this request.
* <li>sessionToken: A valid session token, used for making a request on
* behalf of a specific user.
* <li>json: Return raw JSON without converting to Parse.Object.
* </ul>
* @returns {Promise} A promise that is resolved with the results when
* the query completes.
Expand Down Expand Up @@ -924,33 +925,9 @@ class ParseQuery {
return Promise.reject(error);
}

const query = new ParseQuery(this.className);
query._limit = options.batchSize || 100;
query._include = [...this._include];
query._exclude = [...this._exclude];
if (this._select) {
query._select = [...this._select];
}
query._hint = this._hint;
query._where = {};
for (const attr in this._where) {
const val = this._where[attr];
if (Array.isArray(val)) {
query._where[attr] = val.map(v => {
return v;
});
} else if (val && typeof val === 'object') {
const conditionMap = {};
query._where[attr] = conditionMap;
for (const cond in val) {
conditionMap[cond] = val[cond];
}
} else {
query._where[attr] = val;
}
}

const query = ParseQuery.fromJSON(this.className, this.toJSON());
query.ascending('objectId');
query._limit = options.batchSize || 100;

const findOptions = ParseObject._getRequestOptions(options);
let finished = false;
Expand All @@ -965,7 +942,11 @@ class ParseQuery {
Promise.resolve(previousResults.length > 0 && callback(previousResults)),
]);
if (results.length >= query._limit) {
query.greaterThan('objectId', results[results.length - 1].id);
if (findOptions.json) {
query.greaterThan('objectId', (results[results.length - 1] as any).objectId);
} else {
query.greaterThan('objectId', results[results.length - 1].id);
}
previousResults = results;
} else if (results.length > 0) {
await Promise.resolve(callback(results));
Expand Down
13 changes: 13 additions & 0 deletions src/__tests__/ParseQuery-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1842,6 +1842,7 @@ describe('ParseQuery', () => {
useMasterKey: true,
sessionToken: '1234',
batchSize: 50,
json: true,
};
const q = new ParseQuery('Item');
await q.findAll(batchOptions);
Expand All @@ -1855,6 +1856,7 @@ describe('ParseQuery', () => {
});
expect(options.useMasterKey).toBe(true);
expect(options.sessionToken).toEqual('1234');
expect(options.json).toEqual(true);
});

it('only makes one request when the results fit in one page', async () => {
Expand All @@ -1874,6 +1876,17 @@ describe('ParseQuery', () => {
const results = await q.findAll();
expect(results.map(obj => obj.attributes.size)).toEqual(['medium', 'small']);
});

it('Returns all objects with json', async () => {
const q = new ParseQuery('Item');
const results = await q.findAll({ json: true, batchSize: 2 });
expect(results.length).toEqual(3);
expect(findMock).toHaveBeenCalledTimes(2);
results.map(result => {
expect(result.id).toBeUndefined();
expect(result.objectId).toBeDefined();
});
});
});

it('can iterate over results with each()', done => {
Expand Down