Skip to content

Commit 42e9676

Browse files
authored
fix(Query): correct batch format and add error handler (#443)
1 parent ece8171 commit 42e9676

File tree

3 files changed

+27
-16
lines changed

3 files changed

+27
-16
lines changed

src/query.js

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -240,9 +240,27 @@ module.exports = function(AV) {
240240
}
241241
return obj;
242242
},
243-
_createRequest: function(params, options){
244-
return AVRequest('classes', this.className, null, "GET",
245-
params || this.toJSON(), options);
243+
_createRequest(params = this.toJSON(), options) {
244+
if (JSON.stringify(params).length > 2000) {
245+
const body = {
246+
requests: [{
247+
method: 'GET',
248+
path: `/1.1/classes/${this.className}`,
249+
params,
250+
}],
251+
};
252+
return AVRequest('batch', null, null, 'POST', body, options)
253+
.then(response => {
254+
const result = response[0];
255+
if (result.success) {
256+
return result.success;
257+
}
258+
const error = new Error(result.error.error || 'Unknown batch error');
259+
error.code = result.error.code;
260+
throw error;
261+
});
262+
}
263+
return AVRequest('classes', this.className, null, "GET", params, options);
246264
},
247265

248266
/**
@@ -255,7 +273,7 @@ module.exports = function(AV) {
255273
find: function(options) {
256274
var self = this;
257275

258-
var request = this._createRequest(null, options);
276+
var request = this._createRequest(undefined, options);
259277

260278
return request.then(function(response) {
261279
return _.map(response.results, function(json) {

src/request.js

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -278,16 +278,6 @@ const AVRequest = (route, className, objectId, method, dataObject = {}, authOpti
278278
}
279279
return getServerURLPromise.then(() => {
280280
const apiURL = createApiUrl(route, className, objectId, method, dataObject);
281-
// prevent URI too long
282-
if (apiURL.length > 2000 && method.toLowerCase() === 'get') {
283-
const body = {
284-
request: {
285-
method,
286-
path: apiURL,
287-
},
288-
};
289-
return AVRequest('batch', null, null, 'POST', body, authOptions);
290-
}
291281
return setHeaders(authOptions).then(
292282
headers => ajax(method, apiURL, dataObject, headers)
293283
.then(

test/query.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,11 @@ describe('Queries', function () {
210210

211211
it('containsAll with an large array should not cause URI too long', () => {
212212
return new AV.Query(GameScore)
213-
.containsAll('test', new Array(1000).fill('5821abe62e958a00540046d5'))
214-
.find();
213+
.containsAll('arr', new Array(200).fill('contains-all-test'))
214+
.find()
215+
.then(gameScores => {
216+
gameScores.should.have.length(1);
217+
});
215218
});
216219
});
217220

0 commit comments

Comments
 (0)