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 ?
1 person has
this question
+1
Reply
  • 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...
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. kidding, amused, unsure, silly indifferent, undecided, unconcerned happy, confident, thankful, excited sad, anxious, confused, frustrated

  • 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[i].emailClient; // get client email

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

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

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

    success: onSuccessHandlerFindCliente, // need to extract the index here...
    complete: onCompleteHandlerFindCliente,
    error: onErrorHandlerFindCliente
    });
    }
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. kidding, amused, unsure, silly indifferent, undecided, unconcerned happy, confident, thankful, excited sad, anxious, confused, frustrated

  • 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.
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. kidding, amused, unsure, silly indifferent, undecided, unconcerned happy, confident, thankful, excited sad, anxious, confused, frustrated

  • 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?
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. kidding, amused, unsure, silly indifferent, undecided, unconcerned happy, confident, thankful, excited sad, anxious, confused, frustrated

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

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

    so your code should be like:
    for (i = 0; i < orders.length; i++) { 
    clientEmail = orders[i].emailClient; // get client email

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

    // call service to find client by email
    (function(i, queryEmail){
    findClientebyEmail.execute({
    data: {
    where: JSON.parse(queryEmail),
    orderCounter: i // pass the index to the findClientebyEmail service
    },
    success: function(data){
    console.log(i); // process this value as you need
    },
    complete: onCompleteHandlerFindCliente,
    error: onErrorHandlerFindCliente
    });
    })(i, queryEmail);
    }
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. sad, anxious, confused, frustrated kidding, amused, unsure, silly happy, confident, thankful, excited indifferent, undecided, unconcerned

  • 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 */

    }
    }
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. kidding, amused, unsure, silly indifferent, undecided, unconcerned happy, confident, thankful, excited sad, anxious, confused, frustrated

  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. kidding, amused, unsure, silly indifferent, undecided, unconcerned happy, confident, thankful, excited sad, anxious, confused, frustrated