Skip to content

Commit f6ede67

Browse files
Merge pull request #362 from arfanliaqat/master
🐛 fix 404 on patch call
2 parents 4f742e5 + a063740 commit f6ede67

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ For more information on MongoDB's collation feature, visit the [collation refere
298298
299299
This adapter includes support to enable database transaction to rollback the persisted records for any error occured for a api call. This requires [Mongo-DB v4.x](https://docs.mongodb.com/manual/) installed and [replica-set](https://linode.com/docs/databases/mongodb/create-a-mongodb-replica-set/#start-replication-and-add-members) enabled.
300300
301-
Start working with transactin enabled by adding the following lines in `app.hooks.js` or `<any-service>.hooks.js`.
301+
Start working with transaction enabled by adding the following lines in `app.hooks.js` or `<any-service>.hooks.js`.
302302
303303
```js
304304
const TransactionManager = require('feathers-mongoose').TransactionManager;

lib/service.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -224,14 +224,14 @@ class Service extends AdapterService {
224224

225225
_patch (id, data, params = {}) {
226226
const { query } = this.filterQuery(params);
227-
const mapIds = data => data.map(current => current[this.id]);
227+
const mapIds = data => Array.isArray(data) ? data.map(current => current[this.id]) : [data[this.id]];
228228

229229
// By default we will just query for the one id. For multi patch
230230
// we create a list of the ids of all items that will be changed
231231
// to re-query them after the update
232-
const ids = id === null ? this._find(Object.assign({}, params, {
232+
const ids = this._getOrFind(id, Object.assign({}, params, {
233233
paginate: false
234-
})).then(mapIds) : Promise.resolve([id]);
234+
})).then(mapIds);
235235

236236
// Handle case where data might be a mongoose model
237237
if (typeof data.toObject === 'function') {
@@ -268,7 +268,7 @@ class Service extends AdapterService {
268268
const { query: { $populate } = {} } = params;
269269
// Create a new query that re-queries all ids that
270270
// were originally changed
271-
const updatedQuery = (idList.length && id === null) ? { [this.id]: { $in: idList } } : params.query;
271+
const updatedQuery = { [this.id]: { $in: idList } };
272272
const findParams = Object.assign({}, params, {
273273
paginate: false,
274274
query: $populate ? Object.assign(updatedQuery, { $populate }) : updatedQuery
@@ -286,6 +286,9 @@ class Service extends AdapterService {
286286
if (options.writeResult) {
287287
return writeResult;
288288
}
289+
if ('upserted' in writeResult) {
290+
return this._getOrFind(id, Object.assign({}, params, { query: { [this.id]: { $in: writeResult.upserted.map(doc => doc._id) } } }, { paginate: false }));
291+
}
289292
return this._getOrFind(id, findParams);
290293
});
291294
}).then(select(params, this.id)).catch(errorHandler);

0 commit comments

Comments
 (0)