From abd12a4607f42ef6fd3844d292197cea671aaaea Mon Sep 17 00:00:00 2001 From: Joarley Santos Date: Sat, 26 Dec 2015 21:00:24 -0200 Subject: [PATCH 1/2] Fix remove and exist with _id type different of ObjectId --- lib/index.js | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/lib/index.js b/lib/index.js index a892c55..e4b7487 100644 --- a/lib/index.js +++ b/lib/index.js @@ -85,14 +85,12 @@ Grid.prototype.collection = function (name) { */ Grid.prototype.remove = function (options, callback) { - var _id; if (options._id) { - _id = this.tryParseObjectId(options._id) || options._id; + options._id = this.tryParseObjectId(options._id) || options._id; } - if (!_id) { - _id = options.filename; - } - return this.mongo.GridStore.unlink(this.db, _id, options, callback); + + var gridStore = new this.mongo.GridStore(this.db, options._id, options.filename || null, "w", options, callback); + return gridStore.unlink(callback); } /** @@ -103,14 +101,13 @@ Grid.prototype.remove = function (options, callback) { */ Grid.prototype.exist = function (options, callback) { - var _id; - if (options._id) { - _id = this.tryParseObjectId(options._id) || options._id; - } - if (!_id) { - _id = options.filename; - } - return this.mongo.GridStore.exist(this.db, _id, options.root, callback); + var query = {}; + if (options._id) + query._id = this.tryParseObjectId(options._id) || options._id; + if (options.filename) + query.filename = options.filename; + + return this.mongo.GridStore.exist(this.db, query, options.root, options, callback); } /** From c0aadee8129fe47381d05b8c1e8405f43a19c8e1 Mon Sep 17 00:00:00 2001 From: Joarley Santos Date: Sun, 6 Mar 2016 18:56:43 -0300 Subject: [PATCH 2/2] Fixed bad practice - Mutating input object argument --- lib/index.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/index.js b/lib/index.js index e4b7487..5cd0b26 100644 --- a/lib/index.js +++ b/lib/index.js @@ -85,11 +85,12 @@ Grid.prototype.collection = function (name) { */ Grid.prototype.remove = function (options, callback) { + var _id = null; if (options._id) { - options._id = this.tryParseObjectId(options._id) || options._id; + _id = this.tryParseObjectId(options._id) || options._id; } - var gridStore = new this.mongo.GridStore(this.db, options._id, options.filename || null, "w", options, callback); + var gridStore = new this.mongo.GridStore(this.db, _id, options.filename || null, "w", options, callback); return gridStore.unlink(callback); }