diff --git a/src/service.js b/src/service.js index 854529b4..c8d63fe8 100644 --- a/src/service.js +++ b/src/service.js @@ -135,10 +135,15 @@ class Service { } update(id, data, params) { - if(id === null) { + if (id === null) { return Promise.reject('Not replacing multiple records. Did you mean `patch`?'); } + // Handle case where data might be a mongoose model + if (typeof data.toObject === 'function') { + data = data.toObject(); + } + const options = Object.assign({ new: true, overwrite: this.overwrite, @@ -170,6 +175,13 @@ class Service { patch(id, data, params) { params.query = params.query || {}; + + // Handle case where data might be a mongoose model + if (typeof data.toObject === 'function') { + data = data.toObject(); + } + + // ensure we are working on a copy data = Object.assign({}, data); // If we are updating multiple records diff --git a/test/index.test.js b/test/index.test.js index 03d6fc54..855d55d4 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -182,6 +182,24 @@ describe('Feathers Mongoose Service', () => { }).catch(done); }); + it('can patch a mongoose model', function (done) { + people.get(_ids.Doug).then(dougModel => { + people.patch(_ids.Doug, dougModel).then(data => { + expect(data.name).to.equal('Doug'); + done(); + }).catch(done); + }).catch(done); + }); + + it('can patch a mongoose model', function (done) { + people.get(_ids.Doug).then(dougModel => { + people.update(_ids.Doug, dougModel).then(data => { + expect(data.name).to.equal('Doug'); + done(); + }).catch(done); + }).catch(done); + }); + it('can $push an item onto an array with update', function(done) { pets.create({ type: 'cat', name: 'Margeaux' }).then(margeaux => { people.update(_ids.Doug, { $push: { pets: margeaux } })