Page 1 of 1

execute a service in javascript

Posted: Wed Jul 09, 2014 5:50 pm
by Terry Gilliver

I need to add multiple records (rows) to a collection 'LineItems' after a record has been added to the 'Orders' collection. I propose to do that in the 'success' event of the 'create_order' service.

code
console.log('Order table written successfully');
// now need to add the order items
var item;
var quantity;
for (i = 1; i < shoppingBasket&#46;length; i++) {
item = shoppingBasket[0];
console&#46;log("item = " + item);
quantity = shoppingBasket[1];
console&#46;log("quantity = " + quantity);
&#47;&#47; add record (execute service)
add_order_item&#46;execute({});
}
/code
These are the collections:

Image
Image
Image
Image

I am unsure how to format the parameters for the service. bearing in mind that some of them are pointers.

Also will the looped services execute synchronously(one after the other)? although since these do not reply on each other, it probably doesn't matter.


execute a service in javascript

Posted: Wed Jul 09, 2014 6:31 pm
by Evgene Karachevtsev

Hello Terry,

1) Please take a look here: https://getsatisfaction.com/apperyio/...?
2) The services should be performed asynchronously (in parallel).
3) Pointer should look like this:

code{"collName": "NameOfCollection", "explained": "23480293480234"}/code

where NameOfCollection is collection name pointed to by pointer
23480293480234 - valid (existing) _id of entry on which pointer will point


execute a service in javascript

Posted: Thu Jul 10, 2014 12:11 pm
by Terry Gilliver

shouldn't "explained" be "_id" (which is the column name)?

Also, to get the id from the other collection, I think I need to do a query service on the other collection, something like:

code
get_product&#46;execute({"where":{"Item": item}});
/code


Assuming the syntax is correct, how do I read the values passed in the response?


execute a service in javascript

Posted: Thu Jul 10, 2014 8:11 pm
by Evgene Karachevtsev

Terry,

The format of the data transmission to the service is not correct. Please look at this link:
https://getsatisfaction.com/apperyio/...
All the data returned by the service will be available on the success event of service in the variable data


execute a service in javascript

Posted: Fri Jul 11, 2014 7:13 pm
by Terry Gilliver

From what I've read, I should have something like the following:
code
&#47;&#47;
&#47;&#47;This is the success event of the create_order service
&#47;&#47;
console&#46;log('Order table written successfully');
&#47;&#47; now need to add the order items

&#47;&#47;success function for get_product_id
&#47;&#47;this function is not in-line as the service is executed in a loop (more overhead)
function successFunction(){
&#47;&#47;read the response data here&#46;&#46;&#46;
}

var item;
var quantity;
for (i = 1; i < shoppingBasket&#46;length; i++) {
&#47;&#47;get item and quantity
item = shoppingBasket[0];
console&#46;log("item = " + item);
quantity = shoppingBasket[1];
console&#46;log("quantity = " + quantity);
&#47;&#47;get product item id from item
get_product_id&#46;execute(
{data:
{"where":
{"Item":item},
success: successFunction()
}
}
);
}
/code
a) Can you verify that I am on the right lines?
b) How exactly is the response data accessed in the success event?


execute a service in javascript

Posted: Fri Jul 11, 2014 9:48 pm
by Alena Prykhodko

Hello,

Please check your code, should be something like:
pre

function successFunction(data){
&#47;&#47;read the response data here&#46;&#46;&#46;
}

var item;
var quantity;
for (i = 1; i < shoppingBasket&#46;length; i++) {
&#47;&#47;get item and quantity
item = shoppingBasket[0];
console&#46;log("item = " + item);
quantity = shoppingBasket[1];
console&#46;log("quantity = " + quantity);
&#47;&#47;get product item id from item
get_product_id&#46;execute( {
data: {"where": '{"Item":"'+item+'"}'},
success: successFunction(data)
});
}
/pre