Skip to content

Commit 83a0d7b

Browse files
TysonAndreflovilmart
authored andcommitted
Fix "undefined property '__op'" in postgres update (#4541)
* Fix "undefined property '__op'" in postgres update This causes a TypeError which becomes a regular Error, before the update can be issued. (I think) This happens when there is an object schema, and there is also an unrelated field in originalUpdate which is null or undefined. e.g. when 'location' is a mandatory object in postgres, and 'middleName' is an optional string, PostgresStorageAdapter would throw when a query similar to the below was performed: (Object.keys(originalUpdate) would include "middleName" as a value of `k`) query.set('location', {'country': 'US'}) query.set('middleName', undefined); * Fix lint error
1 parent db8594d commit 83a0d7b

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

src/Adapters/Storage/Postgres/PostgresStorageAdapter.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1200,7 +1200,11 @@ export class PostgresStorageAdapter implements StorageAdapter {
12001200
// Gather keys to increment
12011201
const keysToIncrement = Object.keys(originalUpdate).filter(k => {
12021202
// choose top level fields that have a delete operation set
1203-
return originalUpdate[k].__op === 'Increment' && k.split('.').length === 2 && k.split(".")[0] === fieldName;
1203+
// Note that Object.keys is iterating over the **original** update object
1204+
// and that some of the keys of the original update could be null or undefined:
1205+
// (See the above check `if (fieldValue === null || typeof fieldValue == "undefined")`)
1206+
const value = originalUpdate[k];
1207+
return value && value.__op === 'Increment' && k.split('.').length === 2 && k.split(".")[0] === fieldName;
12041208
}).map(k => k.split('.')[1]);
12051209

12061210
let incrementPatterns = '';
@@ -1216,8 +1220,9 @@ export class PostgresStorageAdapter implements StorageAdapter {
12161220
}
12171221

12181222
const keysToDelete = Object.keys(originalUpdate).filter(k => {
1219-
// choose top level fields that have a delete operation set
1220-
return originalUpdate[k].__op === 'Delete' && k.split('.').length === 2 && k.split(".")[0] === fieldName;
1223+
// choose top level fields that have a delete operation set.
1224+
const value = originalUpdate[k];
1225+
return value && value.__op === 'Delete' && k.split('.').length === 2 && k.split(".")[0] === fieldName;
12211226
}).map(k => k.split('.')[1]);
12221227

12231228
const deletePatterns = keysToDelete.reduce((p, c, i) => {

src/Routers/UsersRouter.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ export class UsersRouter extends ClassesRouter {
268268
throw new Parse.Error(Parse.Error.EMAIL_NOT_FOUND, `No user found with email ${email}`);
269269
}
270270
const user = results[0];
271-
271+
272272
// remove password field, messes with saving on postgres
273273
delete user.password;
274274

0 commit comments

Comments
 (0)