Skip to content

Commit 04c3a32

Browse files
committed
fix: after save on Parse.Role
1 parent 115e76e commit 04c3a32

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

spec/ParseRole.spec.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,4 +601,34 @@ describe('Parse Role testing', () => {
601601
});
602602
});
603603
});
604+
605+
it('should trigger afterSave hook when using Parse.Role class reference', done => {
606+
let afterSaveCalled = false;
607+
608+
Parse.Cloud.afterSave(Parse.Role, req => {
609+
afterSaveCalled = true;
610+
expect(req.object).toBeDefined();
611+
expect(req.object.get('name')).toBe('AnotherTestRole');
612+
});
613+
614+
const acl = new Parse.ACL();
615+
acl.setPublicReadAccess(true);
616+
const role = new Parse.Role('AnotherTestRole', acl);
617+
618+
role
619+
.save({}, { useMasterKey: true })
620+
.then(savedRole => {
621+
expect(savedRole.id).toBeDefined();
622+
// Give the afterSave hook some time to execute
623+
return new Promise(resolve => setTimeout(resolve, 100));
624+
})
625+
.then(() => {
626+
expect(afterSaveCalled).toBe(true);
627+
done();
628+
})
629+
.catch(err => {
630+
fail(`Should not have failed: ${err.message}`);
631+
done();
632+
});
633+
});
604634
});

src/RestWrite.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1743,6 +1743,12 @@ RestWrite.prototype.buildParseObjects = function () {
17431743
const readOnlyAttributes = className.constructor.readOnlyAttributes
17441744
? className.constructor.readOnlyAttributes()
17451745
: [];
1746+
// For _Role class, the 'name' field is read-only after the object has been saved
1747+
// Since _handleSaveResponse is called after buildParseObjects and sets the objectId,
1748+
// we need to exclude 'name' from being set to avoid "A role's name can only be set before it has been saved" error
1749+
if (this.className === '_Role' && !readOnlyAttributes.includes('name')) {
1750+
readOnlyAttributes.push('name');
1751+
}
17461752
if (!this.originalData) {
17471753
for (const attribute of readOnlyAttributes) {
17481754
extraData[attribute] = this.data[attribute];

0 commit comments

Comments
 (0)