3 Replies Latest reply on Nov 21, 2014 1:11 PM by mwringe

    Resource Triggered Business Logic Scripts

    jchase2

      Working with LiveOak 1.0.0.Beta01  I am trying to use Resource Triggered scripts and am immediately running into problems. Following the examples I am trying to use request.resource.properties to get paramters I'm trying to pass to the backend. However, when the function preRead(request, libraries) { } runs, request.resource is undefined. I'm unsure where to go from here. Can anyone point out anything I'm doing wrong please.

        • 1. Re: Resource Triggered Business Logic Scripts
          mwringe

          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

            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

              You need to have the 'client' library added for your script, otherwise it will be empty:

               

              liveoak-example-helloscript/metadata.json at master · liveoak-io/liveoak-example-helloscript · GitHub

               

              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.