-
1. Re: Resource Triggered Business Logic Scripts
mwringe Nov 21, 2014 11:59 AM (in response to jchase2)Pre operations occurs before a resource is accessed, so it can't know the contents of the resource is yet. Only after a resource has been accessed, like on the Post operations, will request.resource be populated with the value.
[This is a bit confusing, we already have the resource as part of the response, so having it as part of the request is redundant on a Post operation, and always empty on a Pre operation. This is a bad design on our part and should be fixed]
If you need to access the resource before reading it, then you will need to read the resource using the client. So something like this would work (note: not actually tried)
function preRead(request, libraries) {
var client = libraries.client;
var resource = client.read(request.path);
...
}
-
2. Re: Resource Triggered Business Logic Scripts
jchasecrafton Nov 21, 2014 12:37 PM (in response to mwringe)Thank you!! I've tried this and found on a preRead request the libraries parameter is just an empty object {}. I'm just starting to use server side scripting so maybe I am not taking the right approach. Essentially I want to pass an id as a parameter and use that id to pull data from another collection and return a json object with the related data I need. I'm just acquainting myself with mongodb too so maybe I'm way off in my approach. Any other thoughts would be greatly appreciated! Thank you!
-
3. Re: Resource Triggered Business Logic Scripts
mwringe Nov 21, 2014 1:11 PM (in response to jchasecrafton)You need to have the 'client' library added for your script, otherwise it will be empty:
Also, part of my last reply was incorrect. When you are doing a read or a delete, you are not sending across a representation of a resource. So the request.resource will be null in this case. When you are doing a create or update, you are sending across a payload which represents a resource. In this case the request.resource will represent the resource to be created or modified.