From c8acc109f8f41d572e8745f16e542454bbd486c7 Mon Sep 17 00:00:00 2001 From: Nick Martin Date: Wed, 15 Jan 2014 15:50:17 -0800 Subject: [PATCH] Pass through the return value for update and remove on validated operations. Fixes #1759. --- packages/mongo-livedata/allow_tests.js | 18 ++++++++++++++++++ packages/mongo-livedata/collection.js | 8 ++++---- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/packages/mongo-livedata/allow_tests.js b/packages/mongo-livedata/allow_tests.js index f30dfc17fe..17439173b4 100644 --- a/packages/mongo-livedata/allow_tests.js +++ b/packages/mongo-livedata/allow_tests.js @@ -421,6 +421,7 @@ if (Meteor.isClient) { {$set: {updated: true}}, expect(function (err, res) { test.isFalse(err); + test.equal(res, 1); test.equal(collection.find({updated: true}).count(), 1); })); }, @@ -431,6 +432,7 @@ if (Meteor.isClient) { {$set: {updated: true}}, expect(function (err, res) { test.isFalse(err); + test.equal(res, 1); test.equal(collection.find({updated: true}).count(), 2); })); }, @@ -603,6 +605,7 @@ if (Meteor.isClient) { canUpdateId, {$set: {"dotted.field": 1}}, expect(function (err, res) { test.isFalse(err); + test.equal(res, 1); test.equal(collection.findOne(canUpdateId).dotted.field, 1); })); }, @@ -622,6 +625,7 @@ if (Meteor.isClient) { {$set: {updated: true}}, expect(function (err, res) { test.isFalse(err); + test.equal(res, 0); // nothing has changed test.equal(collection.find().count(), 3); test.equal(collection.find({updated: true}).count(), 0); @@ -670,6 +674,7 @@ if (Meteor.isClient) { {$set: {updated: true}}, expect(function (err, res) { test.isFalse(err); + test.equal(res, 1); test.equal(collection.find({updated: true}).count(), 1); })); }, @@ -701,6 +706,7 @@ if (Meteor.isClient) { {$set: {cantRemove: false, canUpdate2: true}}, expect(function (err, res) { test.isFalse(err); + test.equal(res, 1); test.equal(collection.find({cantRemove: true}).count(), 0); })); }, @@ -710,11 +716,23 @@ if (Meteor.isClient) { collection.remove(canRemoveId, expect(function (err, res) { test.isFalse(err); + test.equal(res, 1); // successfully removed test.equal(collection.find().count(), 2); })); }, + // try to remove a doc that doesn't exist. see we remove no docs. + function (test, expect) { + collection.remove('some-random-id-that-never-matches', + expect(function (err, res) { + test.isFalse(err); + test.equal(res, 0); + // nothing removed + test.equal(collection.find().count(), 2); + })); + }, + // methods can still bypass restrictions function (test, expect) { collection.callClearMethod( diff --git a/packages/mongo-livedata/collection.js b/packages/mongo-livedata/collection.js index 0c91c52a42..0fbadc0ae3 100644 --- a/packages/mongo-livedata/collection.js +++ b/packages/mongo-livedata/collection.js @@ -780,7 +780,7 @@ Meteor.Collection.prototype._validatedUpdate = function( var doc = self._collection.findOne(selector, findOptions); if (!doc) // none satisfied! - return; + return 0; var factoriedDoc; @@ -813,7 +813,7 @@ Meteor.Collection.prototype._validatedUpdate = function( // avoid races, but since selector is guaranteed to already just be an ID, we // don't have to any more. - self._collection.update.call( + return self._collection.update.call( self._collection, selector, mutator, options); }; @@ -843,7 +843,7 @@ Meteor.Collection.prototype._validatedRemove = function(userId, selector) { var doc = self._collection.findOne(selector, findOptions); if (!doc) - return; + return 0; // call user validators. // Any deny returns true means denied. @@ -864,5 +864,5 @@ Meteor.Collection.prototype._validatedRemove = function(userId, selector) { // Mongo to avoid races, but since selector is guaranteed to already just be // an ID, we don't have to any more. - self._collection.remove.call(self._collection, selector); + return self._collection.remove.call(self._collection, selector); };