From ef4cc3efc28bc6c12e7c8a1506b29eab4fb190ba Mon Sep 17 00:00:00 2001 From: CHiCO Date: Wed, 17 Mar 2021 12:41:30 +0800 Subject: [PATCH] fix: throws when fetching object without id (#641) --- src/file.js | 3 +++ src/object.js | 3 +++ test/file.js | 4 ++++ test/object.js | 4 ++++ 4 files changed, 14 insertions(+) diff --git a/src/file.js b/src/file.js index b20fc6ea1..233befd77 100644 --- a/src/file.js +++ b/src/file.js @@ -619,6 +619,9 @@ module.exports = function(AV) { * completes. */ fetch(fetchOptions, options) { + if (!this.id) { + throw new Error('Cannot fetch unsaved file'); + } var request = AVRequest( 'files', null, diff --git a/src/object.js b/src/object.js index 9b7984073..bf8b397b5 100644 --- a/src/object.js +++ b/src/object.js @@ -946,6 +946,9 @@ module.exports = function(AV) { * completes. */ fetch: function(fetchOptions = {}, options) { + if (!this.id) { + throw new Error('Cannot fetch unsaved object'); + } var self = this; var request = _request( 'classes', diff --git a/test/file.js b/test/file.js index 7bbfe2f7f..89fc74b06 100644 --- a/test/file.js +++ b/test/file.js @@ -188,6 +188,10 @@ describe('File', function() { .fetch() .then(file => (this.file = file)); }); + it('should throws when objectId is empty', () => { + const file = new AV.File('filename', { base64: 'dGVzdA==' }); + expect(file.fetch).throwError(); + }); it('should retrieve all data', function() { var file = this.file; expect(file).to.be.a(AV.File); diff --git a/test/object.js b/test/object.js index 096ff6307..a97fc5971 100644 --- a/test/object.js +++ b/test/object.js @@ -447,6 +447,10 @@ describe('Objects', function() { expect(score.createdAt).to.be.a(Date); expect(score.id).to.be.eql(gameScore.id); })); + it('should throws when objectId is empty', () => { + const object = new AV.Object('GameScore'); + expect(object.fetch).throwError(); + }); it('fetch should remove deleted keys', () => { const getFakedScore = () => AV.parseJSON(