Joni de Campos
Posts: 0
Joined: Fri Dec 11, 2015 12:21 pm

Pass parameters to a cascade of services using javascript ( serviceName.execute())

I have a Order_list_service that get all orders that I received.

For each order a need to use a signup_service, which return the new user _id.

I then have to use a client_create_service to register the new user to client collection. In this client_create_service a need data from the Order_list_service. To easyly get that for each Order, a need to pass the order index to the signup_service, which call the client_create_service which aldo need the order index from the Order_list_service.

So for Order index1, I call signup_service and then need to pass index1, to the cliente_create_service. This way the next service knows that I am processing the index1 order, etc...

Since Order_list is a loop, several signup_services are call at once...

To avoid that I only call the signup_service at the complete of the Order_list_service.

To garantee that the correct child service receives the correct parameter ( Order.useremail for example) to use it in th signup_service.

Some times it seems not to work.

Is there a way to tell the system to wait till the parent service to finish the service, or the parent service always will trigger the onComplete event when it finishes the service run?

Any ideia ?

Serhii Kulibaba
Posts: 150
Joined: Tue Aug 27, 2013 1:47 pm

Pass parameters to a cascade of services using javascript ( serviceName.execute())

Hello Joni,

You have to use the self-executing function there and pass the variable to that function or use the solution, described here: http://www.albertgao.xyz/2016/08/25/w...

Joni de Campos
Posts: 0
Joined: Fri Dec 11, 2015 12:21 pm

Pass parameters to a cascade of services using javascript ( serviceName.execute())

Hi Serhii,

When you say " You have to use the self-executing function there and pass the variable", I understand that a have to pass the variable in the data call, right ?

This is the first level loop. I am trying to pass the i index to findClientebyEmail service.

But the question is how do I extract the i index variable onSuccess handler Or onComplete handler?

CODE:

//orders is the array that contains the return data from Order_list_service
for (i = 0; i < orders.length; i++)
{
clientEmail = orders.emailClient; // get client email

queryEmail = '{"email":"' + clientEmail + '"}';

// call service to find client by email
findClientebyEmail.execute({
data: {
where: JSON.parse(queryEmail),

Code: Select all

orderCounter: i // pass the index to the findClientebyEmail service 

},

success: onSuccessHandlerFindCliente, // need to extract the index here...
complete: onCompleteHandlerFindCliente,
error: onErrorHandlerFindCliente
});
}

Serhii Kulibaba
Posts: 150
Joined: Tue Aug 27, 2013 1:47 pm

Pass parameters to a cascade of services using javascript ( serviceName.execute())

Do you need to use the value of the variable "i" in the function "onSuccessHandlerFindCliente"?

Is it your own service, based on Server Code or you just use the API?

If you use your own Server Code, you can pass the value "i" back in the response of that service.

Joni de Campos
Posts: 0
Joined: Fri Dec 11, 2015 12:21 pm

Pass parameters to a cascade of services using javascript ( serviceName.execute())

Yes..."i" is necessary to the function onSuccessHandlerFindCliente. Since orders is a global variable, all information we need is there...we only need the index do orders to get user order information like email, etc.

The variable "i" is used inside this function to get the user data from the orders array and call signup_service to insert this as a new client.

I am using only the database API...

Or can we use promise witha Appery Service ? If so, How?

Serhii Kulibaba
Posts: 150
Joined: Tue Aug 27, 2013 1:47 pm

Pass parameters to a cascade of services using javascript ( serviceName.execute())

  1. Please wrap the call of the service to the self-executing function

    1. Use the function "onSuccessHandlerFindCliente" right here, do not define in somewhere else

      so your code should be like:
      prefor (i = 0; i < orders&#46;length; i++) {
      clientEmail = orders&#46;emailClient; &#47;&#47; get client email

      queryEmail = '{"email":"' + clientEmail + '"}';

      &#47;&#47; call service to find client by email
      (function(i, queryEmail){
      findClientebyEmail&#46;execute({
      data: {
      where: JSON&#46;parse(queryEmail),
      orderCounter: i &#47;&#47; pass the index to the findClientebyEmail service
      },
      success: function(data){
      console&#46;log(i); &#47;&#47; process this value as you need
      },
      complete: onCompleteHandlerFindCliente,
      error: onErrorHandlerFindCliente
      });
      })(i, queryEmail);
      }/pre

Joni de Campos
Posts: 0
Joined: Fri Dec 11, 2015 12:21 pm

Pass parameters to a cascade of services using javascript ( serviceName.execute())

Thanks Serhill,

Since it is a loop, where should I put a delay to not exceed the database api burst rate ?

Can it be the sleep function bellow:

function sleep(millisecondsToWait) {
var now = new Date().getTime();
while (new Date().getTime() < now + millisecondsToWait) {
/* do nothing; this will exit once it reaches the time limit */

Code: Select all

 } 

}

Serhii Kulibaba
Posts: 150
Joined: Tue Aug 27, 2013 1:47 pm

Pass parameters to a cascade of services using javascript ( serviceName.execute())

  1. Please use the default function setTimeout (https://developer.mozilla.org/en-US/d...) instead of that your custom one, I believe it works faster and simple to use
    1. You have to call it inside of the function
      pre&#46;&#46;&#46;
      function(i, queryEmail){
      &#46;&#46;&#46;/pre
      or wrap it with the function setTimeout
Joni de Campos
Posts: 0
Joined: Fri Dec 11, 2015 12:21 pm

Pass parameters to a cascade of services using javascript ( serviceName.execute())

Hi Serhill,

Got the code working ( now i can get the value of i inside the success response.

Now the challenge is to execute the self-executing function only after the previous one is ended...

So i found no way of doing that...the self-executing function executes right after the first call is made, so they behave line in parallel.

Is there a way to call the self-executing function inside the loop only when the success response processing is finish ?

So the next i is processed only when the previous one is finishing processing ?

Return to “Issues”