How to call the platform.api.get() in user.session.post.post_process?


#1

I’m trying to append the venue_id with user/session returned resource. I have used the following script in my user.session.post.post_process

var lodash = require(“lodash.min.js”);

//throw platform.session.session_token;
var x = '"X-Dreamfactory-Session-Token:"'+ platform.session.session_token +'"';
//throw x;
var options = {
"HEADERS": ["Content-type: application/json", x]
};
    var venue = platform.api.get('treat_well/_table/tw_venue',options);
    var id = venue.content.resource[0].venue_id;
throw id;

But it throws

Cannot read property ‘0’ of undefined

I tried the following script in elsewhere except this user.session.post.post_process , It’s working

var lodash = require("lodash.min.js");
    var venue = platform.api.get('treat_well/_table/tw_venue');
    var id = venue.content.resource[0].venue_id;
throw id;

I thought in user/session that there might be session-token header is missed that’s why i have appended the header as follwing in user/session

var x = '"X-Dreamfactory-Session-Token:"'+ platform.session.session_token +'"';
//throw x;
var options = {
"HEADERS": ["Content-type: application/json", x]
};

What’s the mistake that i have missed in user/session post_process
Please help me


#2

You need to get the user session first:

var session = platform.api.get('user/session'); var_dump(session); //dump all to df log for debugging var token = session.content.session_token; //sets var to the token string var_dump("dumping session token");//dump token to log for debugging var_dump(token); var x = '"X-Dreamfactory-Session-Token:"'+ token +'"'; var options = { "HEADERS": ["Content-type: application/json", x] };
var_dump(options);//dump debug options

`


#3

@Mike_Harvey Why not we use the platform.session.session_token to get the current user token ?
If i try your scripts too when i call the

var venue = platform.api.get('treat_well/_table/tw_venue',options);
throw venue.content.resource[0].venue_id

It still gives the error as

Cannot read property ‘0’ of undefined

Why it’s happening there?


#4

Can you say how to debug the scripting execution in DSP? Just i want to check what’s the error is returning while the execution of platform.api.get(‘treat_well/_table/tw_venue’,options); ?

It seems that some header or something gets failure while calling

platform.api.get(‘treat_well/_table/tw_venue’,options); in user/session post_process.

But this api is working when i call in elsewhere except the user/session…


#5

You can use the platform.session,session_token provided you are logged in. I have done a simple test to dump the platform and event objects in the user.session post event and they return null which means you are in fact not logged in (yet) during a user/session post event. Any other post event after session, you’re probably fine. But in this case I think it’s a chicken/egg thing:

var_dump(event) returned:
[27-Feb-2016 16:24:30 UTC] REST Exception #404 > Could not find a service for dreamfactory

and var_dump(platform) returned:
[27-Feb-2016 15:40:11 UTC] REST Exception #400 > Bad request. No token or api key provided.

If you are attempting to append a param to the session response, you will need to log in first (get a session then pass session and api_key to your table call. But since the session you want to modify doesn’t exist during the event, i’m not sure how you would extend it. Good question though (if it’s even possible). I’ve found nothing else on how to do this. Knowing this, I would append the session var after login in a separate call.

As for logging and debugging:
From your dreamfactory install folder to can tail storage/logs/dreamfactory.log. On Bitnami Windows for example its:

C:\Bitnami\dreamfactory-2.1.0-4\apps\dreamfactory\htdocs\storage\logs\dreamfactory.log

var_dumps and errors typically are logged in dreamfactory.log (unless you have significantly customized your install). While debugging i would var_dump and not throw until you are sure you are retrieving what you want, and check the log. That is my preference as I have seen that using throw interferes with var_dumps or debugging output in the log (probably due to event propagation between v8js and php). A DF engineer may have more clarification on this.

Instead of throwing, do this to see what your venue.content.resource is returning (assuming you are logged in):

var_dump(venue.content.resource);

If you don’t see values in your array in the log, that would be the reason for your error.

Hope this helps.


#6

Yes bro var_dump and dreamfactory log are helped to recover from troubles. I have the issue in the following link can you figure out it?