Skip to content

Commit 924dfc0

Browse files
committed
fix(Query): throw NOT_FOUND error when getting a non-existing object
1 parent 5143dbe commit 924dfc0

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

src/query.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const debug = require('debug')('leancloud:query');
33
const Promise = require('./promise');
44
const AVError = require('./error');
55
const AVRequest = require('./request').request;
6-
const { ensureArray } = require('./utils');
6+
const { ensureArray, transformFetchOptions } = require('./utils');
77

88
const requires = (value, message) => {
99
if (value === undefined) {
@@ -187,19 +187,22 @@ module.exports = function(AV) {
187187
throw errorObject;
188188
}
189189

190-
var self = this;
191-
192-
var obj = self._newObject();
190+
var obj = this._newObject();
193191
obj.id = objectId;
194192

195-
var queryJSON = self.toJSON();
193+
var queryJSON = this.toJSON();
196194
var fetchOptions = {};
197195

198196
if (queryJSON.keys) fetchOptions.keys = queryJSON.keys;
199197
if (queryJSON.include) fetchOptions.include = queryJSON.include;
200198
if (queryJSON.includeACL) fetchOptions.includeACL = queryJSON.includeACL;
201199

202-
return obj.fetch(fetchOptions, options);
200+
return AVRequest('classes', this.className, objectId, 'GET', transformFetchOptions(fetchOptions), options)
201+
.then((response) => {
202+
if (_.isEmpty(response)) throw new AVError(AVError.OBJECT_NOT_FOUND, 'Object not found.');
203+
obj._finishFetch(obj.parse(response), true);
204+
return obj;
205+
});
203206
},
204207

205208
/**

test/query.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ describe('Queries', function () {
4141
}).to.throwError();
4242

4343
});
44+
it('should throw when object not exists', function () {
45+
query = new AV.Query(GameScore);
46+
return query.get('123').should.be.rejectedWith(/Object not found/);
47+
});
4448

4549
});
4650

@@ -287,12 +291,11 @@ describe('Queries', function () {
287291

288292
var userQ = new AV.Query('Person');
289293

290-
return userQ.get('52f9bea1e4b035debf88b730').then(function (p) {
291-
p.relation('likes').query();
294+
return userQ.first().then(function (p) {
295+
return p.relation('likes').query().count();
292296
// p.relation('likes').query().count().then(function(c){
293297
// debug(c)
294298
// })
295-
debug(p);
296299
});
297300

298301
// userQ.first().then(function(p){

0 commit comments

Comments
 (0)