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.