//check if u have a payload
if (event.request.payload) {
//check if u have the key snapTypeID
if (!event.request.payload.snapTypeID) {
throw 'snapTypeID cannot be empty';
} else {
//we have the key, so change it
event.request.payload.snapTypeID = 2;
}
}
I am still getting similar kind of issue. I am unable to update resource from event. below here is my event and code within event. I can validate the value but when I update the value of it, its not reflecting in db.
Event: mysql mysql._table.{table_name} mysql._table.{table_name}.post.pre_process mysql._table.employee.post.pre_process
Code:
console.log(event.request); // outputs to file in storage/log of dreamfactory install directory
// use NPM to install lodash
event.request.content_changed = true;
var lodash = require(“lodash”);
event.request.payload.resource[0].salary = 90;
Note: I have enabled both check boxes “Active” and “Allow script to modify request payload”. I am sure that Event is triggering because I can validate the data and throw errors.
Sorry to resurrect this topic, but this process is not at all clear from the documentation, especially for Python scripting. It would be very helpful to add this information to the documentation and provide some examples of how to do this in the example scripts. Maybe I can help?
From reading the documents, I guessed that I needed to update event.request.payload.resource[0], but that has no effect on the payload that is actually inserted into the database. Instead, I need to modify the payload, then do event.request.content = json.dumps(event.request.payload).
Is there a reason that this isn’t an automatic process? When payload is updated and “Allow script to modify request payload” is set to True, shouldn’t content automatically update to match payload?
This script is used as a pre-post request script. It takes the input document, looks for “start”, “end”, “start_datetime”, and “end_datetime” and creates any that don’t exist based on the ones that do. So, if “start” exists as a timestamp, “start_datetime” is created based on that timestamp.
Hi I’m struggling for some weeks against a post_request pre process script that modifies my payload and I found this post that helps me. The dreamfactory documentation is not so clear for me…
If I make a post request with the following payload without any script activated it works great and all the fields are inserted into my sql database:
{"error":{"code":1000,"context":{"error":["Gas"],"resource":{"0":{"messageID":32},"Gas":{"code":500,"message":"array_merge(): Argument #1 is not an array"}}},"message":"Batch Error: Not all requested records could be created.","status_code":500}}
Please could you help me to solve the problem and fix my script? How can I index the resource field that I need to modify in my payload ?