Fred McIntyre
Posts: 0
Joined: Sun Jan 12, 2014 5:04 pm

How to get service response when executed in javascript

In a REST service response parameter, which is an array, I have javascript like this:
predatabase_query_service.execute({userid:value.userid});/pre

I know it works because I tested it with a database delete service.

However, this is a database query service. I tested the service sending just that one parameter, and in the service Test area, it returned an array of all columns where the field userid has the value sent to it.

I tried
prevar ary = database_query_service.execute({userid:value.userid});
var data = JSON.parse(ary);/pre

but that didn't work.

I need to know how to get the returned values in that javascript, because I need to use them in another service execute in the same block of javascript.

Thanks.

Nikita
Posts: 0
Joined: Fri Feb 28, 2014 4:02 pm

How to get service response when executed in javascript

Hello,

You should add a handler to service success event. In this case the service response will be available in "data" variable.

Fred McIntyre
Posts: 0
Joined: Sun Jan 12, 2014 5:04 pm

How to get service response when executed in javascript

I tried using the Success event, however I don't know how to get the values from the array mapping to the handler in the Success event.

Details:
In the database I have a collection with three columns (other than the defaults) named userid, nextid, and previd. I need to be sure that there is only one row for the column userid - that is, userid must be unique.

I have an array coming from the REST API named incoming. Three of the elements in the array are: userid, nextid, and previd. This array is mapped to a single list item named contacts.

In the Response parameters, on contacts I have this Javascript:
prelocalStorage.setItem('currid',value.userid);
localStorage.setItem('nextid',value.nextid);
localStorage.setItem('previd',value.previd);
query_nextprev.execute({'where':'{"userid":"'+value.userid+'"}'});/pre

This query successfully returns the array of the column which contains the value passed to it in the "where"

What I need to do is, if there is a row with that userid, I need to update the values of nextid and previd. If there is not a row with that userid, I need to create one.

In the Success event for query_nextprev I have the following:
prevar cr = localStorage.getItem('currid');
var ne = localStorage.getItem('nextid');
var pr = localStorage.getItem('previd');
alert('cr '+cr+', ne '+ne+', pr '+pr+', id '+data[0].id);
if (data[0].id === '' || data[0].id === undefined) {
create_nextprev.execute({
'userid':cr,
'nextid':ne,
'previd':pr
});
} else {
update_nextprev.execute({
'userid':cr,
'nextid':ne,
'previd':pr,
'object_id':data[0].id
});
}/pre

The alert is there just so I can see what values it is getting. Both the create_nextprev and update_nextprev database service work correctly.

The problem is that each time this Javascript is run, because the REST services are not executed sequentially, the values for the localStorage items are the same every time the Success event is fired. (I am doing it with only two items in the array from my REST service. If I had more, perhaps those values would change occassionally.)

If I could do the query in the Javascript on the mapping event, and then in that same event, get the results of that query, then I could also do the create or update in the same Javascript block.

I know the following won't work, but it is an example of what I'd like to accomplish:
prevar data = query_nextprev.execute({'where':'{"userid":"'+value.userid+'"}'});/pre

and then I'd have the results of the query in data, and I could then make the database services run sequentially. I simply need to know how to capture the results of the database query in the same block of Javascript where it is executed, not in the Success event.

Any suggestions or help will be much appreciated.

Igor
Posts: 0
Joined: Tue Apr 02, 2013 12:05 pm

How to get service response when executed in javascript

If you want handle success event in the same block of javascript you could use next code:

pre
code
// Event handler for your success event.
var onServiceSuccess = function(query_nextprevData){
//Code to process query_nextprevData from query_nextprev success event.
};
query_nextprev.execute({data: {'where':'{"userid":"'+value.userid+'"}'}, success: onSuccessEvent })
/code
/pre

Note:

  1. You need to wrap your parameters to "data" node;
  2. Standard success processing will not execute, if you handle success event manually.
Fred McIntyre
Posts: 0
Joined: Sun Jan 12, 2014 5:04 pm

How to get service response when executed in javascript

Exactly what I needed. Thank you so much. Now, if I can actually implement it, I'll be in business!

Fred McIntyre
Posts: 0
Joined: Sun Jan 12, 2014 5:04 pm

How to get service response when executed in javascript

Igor,
One small correction to your code. In the first line you used var onServiceSuccess, however in the execute you used success:onSuccessEvent.

Those two need to be the same var.

Fred McIntyre
Posts: 0
Joined: Sun Jan 12, 2014 5:04 pm

How to get service response when executed in javascript

Very easy to implement, with the one change noted in my comment to Igor, above.

Fred McIntyre
Posts: 0
Joined: Sun Jan 12, 2014 5:04 pm

How to get service response when executed in javascript

In case this helps someone else, here is the code I ended up with.
Note: query_nextprev, create_nextprev, and update_nextprev are imported database query, create, and update services. In the database there are three columns currid, nextid, previd.

This javascript is in the response parameters of my custom REST API service, where an array is mapped to a list item. This is added to that list item. My goal was to create or update the columns in the database for each item in the array. value.currid, value.nextid, and value.previd are elements of the array returned by my REST service, sent to this function by the mapping.

prevar onServiceSuccess = function(data) {
if (data[0]) {
update_nextprev.execute({
data: {
'currid':value.currid,
'nextid':value.nextid,
'previd':value.previd,
'object_id':data[0]._id
}
});
} else {
create_nextprev.execute({
data: {
'currid':value.currid,
'nextid':value.nextid,
'previd':value.previd
}
});
}
};
query_nextprev.execute({
data: {
'where':'{"userid":"'+value.currid+'"}'
},
success: onServiceSuccess
});/pre

Return to “Issues”