Custom script - Insert query parameters into MongoDB - write to log

Can I have a simple example of how to create a custom script that would insert a record into the local MongoDB? I have already created a role for guest access. I am passing the API key in the URL and calling the custom script with a GET method. Btw I am using v8js so is there a way for me to log the incoming query parameters somewhere? It will make debugging easier. Thanks

Are you able to insert the records you want via API Docs? If so, all you have to do is replicate the same proccess using your script, i.e.:

platform.api.post('/yourmongodfservice/_table/yourtable', {"resource": [{"your": "fields"}]}

Please refer to the docs for more info: https://wiki.dreamfactory.com/DreamFactory/Features/Scripting

To output log messages you can use:

print("Whatever you want to output to log here");

Or

print( "Request data: " + JSON.stringify(request));

Or

var_dump(request); // this one shows prettier views of arrays

The logs are written to [DF INSTALLATION]/htdocs/storage/logs/dreamfactory.log. I suggest you open a terminal and use tail -f in this file to watch in real time the logs you output from your application.

Alright, thanks a lot for the info and links. Btw the custom script is being called by a guest role. I guess I wouldn’t need to pass additional credentials or authorization when calling the api.post in my script. Thanks once again

Btw a related question - what would be the output of the below statement if failed or successful? I need to decide the action based on that.
result = platform.api.post( "cldbgps/_table/positions", positiondata );

Is it a status code like 200, 400 etc or a json string/object? Strange no matter what I try, it isn’t writing anything to the dreamfactory.log.

Even hard coded statements are also not getting written in dreamfactory.log
print("Whatever you want to output to log here");

Thanks

If your app is allowing guest access to your service through the appropriate role you won’t need to provide credentials for authorization. That’s what guest access is meant to be :slight_smile:. But you’ll still need to provide the API_KEY.

If the print command is not printing anything to the log file probably your script is not being executed, or you may have some problems with your v8js installation - the latter for sure won’t be the case if you’re running a Bitnami installation. Try to invoke your service through API Docs to make sure the correct service is really being called. Also note that if you have any errors in your script its execution will be aborted at the point of failure without any warning, so consider using a try...catch block to avoid this situation.

Regarding the results of platform.api.post, please refer to the documentation to get a broader undestanding of its output: https://wiki.dreamfactory.com/DreamFactory/Features/Scripting

What you’re looking for is in the section “Event Response” of the docs. Please note this is suited for post_proccess service execution (you won’t have any results in the pre_proccess yet). You can look into result.event.response.status_code to know the result of your platform.api.post request.

Actually in addition to the API key, I had to assign permission to the resource used in the POST to the requestor Script. My custom script is pretty straightforward actually and every line in the script is working predictably except for the print (which is supposed to write to the log file in storage/logs/dreamfactory.log. What surprises me is things are getting written to the log file but not the result of the print command. I even tried putting it up at the top, as the first line so that it gets executed before anything else does. I think I’ll perhaps have to relook at the permissions first. Thanks