How to Invoke another server code script from a server code script
I have two server code scripts A and B. I want to invoke script A from script B by passing parameters and use the result set for further steps in script B. How do i do that?
Catch up wih the Appery.io community on our forum. Here you'll find information on the lastest questions and issues Appery.io developers are discussing.
https://forum.appery.io/
I have two server code scripts A and B. I want to invoke script A from script B by passing parameters and use the result set for further steps in script B. How do i do that?
Hello!
You need to call it via XMLHttpRequest http://devcenter.appery.io/documentat...
When i am calling the script from another script, both of which are running at server end, why unnecessarily an XHR request to be made?
I was expecting something similar to http://devcenter.appery.io/documentat....
So, in this case, there are two ways to invoke, via an id to the script or by alias name. How do i get the id or alias name from servercode tab for my server side scripts?
ok, I managed to find out id and alias name of a server code script. Now, say for example, A and B are two scripts.
A:
...
var userId = request.user._id;
console.log(userId);
...
B:
...
var userId = request.user._id;
console.log(userId);
var res = ScriptCall.call("A_aliasName", {}, "body text", "text/plain");
...
Now, when I execute B, here it gets the user id from the request. Similarly, when A is executed separately, it gets the value of user id from the request. However, when A is invoked from B, it complaints that
"Cannot read property 'id' of null ( @ 2 : 25 ) - var userId = request.user.id;"
What am i missing?
Hi Girish,
Here is a simple code to RUN other server script by ID. ID you can find in url to this script editor.
pre
var res = ScriptCall.call("dd49c8c9-be34-451e-86bf-7d51e1a8af06", {"key":"value"}, "body text", "text/plain")
console.log(JSON.stringify(res));
/pre
If you want to get response parameter in "A" script you should pass this parameter when invoke the script.
For your case it should be like:
pre
var res = ScriptCall.call("dd49c8c9-be34-451e-86bf-7d51e1a8af06", {"user": {"_id": "1235567"} }, "body text", "text/plain")
console.log(JSON.stringify(res));
/pre
In this way you can pass all parameter you need.
Regards.
Yes, but the way I invoke differs, correct? When I invoke A directly, request.user.id is part of session request header. But when the same is invoked from B, this request header is not passed and hence request.user.id doesn't work. So how do we pass the request headers as well to the called script?
Hi Girish,
Unfortunatly there is no ability to pass headers.
If you need it you can use XHR.send instead of "ScriptCall.call".
Regards.