Terry Gilliver
Posts: 0
Joined: Fri Apr 18, 2014 8:45 pm

execute a service in javascript

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.

Evgene Karachevtsev
Posts: 12
Joined: Mon Apr 28, 2014 1:12 pm

execute a service in javascript

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

Terry Gilliver
Posts: 0
Joined: Fri Apr 18, 2014 8:45 pm

execute a service in javascript

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?

Evgene Karachevtsev
Posts: 12
Joined: Mon Apr 28, 2014 1:12 pm

execute a service in javascript

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

Terry Gilliver
Posts: 0
Joined: Fri Apr 18, 2014 8:45 pm

execute a service in javascript

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?

Alena Prykhodko
Posts: 0
Joined: Tue Apr 09, 2013 7:36 am

execute a service in javascript

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

Return to “Issues”