issue(afterFind): Fixes issue when using afterFind with relations (#4752)
* Adds failing test for the issue * Adds fix for the issue
This commit is contained in:
@@ -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);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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) => {
|
||||
// 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;
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user