Skip to content

Pre-Process Script (NodeJS) Processes Content Block instead of Resource Block #28

@darkpoetcc

Description

@darkpoetcc

According to the documentation (http://wiki.dreamfactory.com/DreamFactory/Tutorials/V8_field_scrambling), I can modify data before it hits an API / Database, such as scrubbing SSN numbers, by using a Pre-Process server-side script. However, no matter what I do in a Pre-Process script, the script never does what it should (a small sample is below). Per the documentation, I should modify the payload's resource records; however, the only way I can get things working is to modify the request's content value. Below, this is all documented.

if (event.request.payload.resource) { 

    // Go through each record and modify appropriately
    if (event.request.payload.resource.length  && event.request.payload.resource.length >= 1) {
        event.request.payload.resource.forEach((record, idx) => {
        
            // Placeholder for the actual request detail
            let request = {};
            
            // Pull data from the API request and format appropriately
            request.JO = record.so.jobOrder;
            request.PanelNo = record.ps.panelNo;

            // This reassignment works, as I see it correct in the below console.log statements
            event.request.payload.resource[idx] = request;
            console.log(record);
            console.log(event.request.payload);
        });
    }
}

According to console.log, here is what I now have:

{
  resource: [
    {
      JO: '2',
      PanelNo: '4',
      DR: '5',
      FK_WorkStatusId: 200
    }
  ]
}

However, according to the error I get back from the DreamFactory API when trying to post, this is not what the data looks like at all:

{
    "error": {
        "code": 1000,
        "context": {
            "error": [
                0
            ],
            "resource": [
                {
                    "code": 400,
                    "context": null,
                    "message": "Required field 'fk_workstatusid' can not be NULL.",
                    "status_code": 400
                }
            ]
        },
        "message": "Batch Error: Not all requested records could be created.",
        "status_code": 500
    }
}

In order to get this feature to work, I have to modify not according to existing DreamFactory documentation, but per my own discovery. For instance, this works:

    // Above I made all of the changes to data I needed to, as my earlier example
    event.request.content = JSON.stringify(event.request.payload.resource);

    // Not having the `event.request.payload.resource` object means nothing for the call to succeed
    // Leaving this to the modified values, deleting the value, or anything in between seems to have no effect on the final operations
    event.request.payload.resource = null;

Either this feature has changed, or there is a bug. Considering I have to take the records of the incoming request payload's resource, modify them, convert them to a string, and then shove them two layers shallower to get the intended effects, I believe something is buggy with this behavior in 3.0.1.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions