Skip to content

issue(afterFind): Fixes issue when using afterFind with relations #4752

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions spec/ParseRelation.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -801,4 +801,37 @@ describe('Parse.Relation testing', () => {
done();
});
});

it('ensures beforeFind on relation doesnt side effect', (done) => {
const parent = new Parse.Object('Parent');
const child = new Parse.Object('Child');
child.save().then(() => {
parent.relation('children').add(child);
return parent.save();
}).then(() => {
// We need to use a new reference otherwise the JS SDK remembers the className for a relation
// After saves or finds
const otherParent = new Parse.Object('Parent');
otherParent.id = parent.id;
return otherParent.relation('children').query().find();
}).then((children) => {
// Without an after find all is good, all results have been redirected with proper className
children.forEach((child) => expect(child.className).toBe('Child'));
// Setup the afterFind
Parse.Cloud.afterFind('Child', (req) => {
return Promise.resolve(req.objects.map((child) => {
child.set('afterFound', true);
return child;
}));
});
const otherParent = new Parse.Object('Parent');
otherParent.id = parent.id;
return otherParent.relation('children').query().find();
}).then((children) => {
children.forEach((child) => {
expect(child.className).toBe('Child');
expect(child.get('afterFound')).toBe(true);
});
}).then(done).catch(done.fail);
});
});
13 changes: 12 additions & 1 deletion src/RestQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,18 @@ RestQuery.prototype.runAfterFindTrigger = function() {
}
// Run afterFind trigger and set the new results
return triggers.maybeRunAfterFindTrigger(triggers.Types.afterFind, this.auth, this.className,this.response.results, this.config).then((results) => {
this.response.results = results;
// Ensure we properly set the className back
if (this.redirectClassName) {
this.response.results = results.map((object) => {
if (object instanceof Parse.Object) {
object = object.toJSON();
}
object.className = this.redirectClassName;
return object;
});
} else {
this.response.results = results;
}
});
};

Expand Down