Jeff Salter
Posts: 0
Joined: Sun Jul 05, 2015 2:30 pm

Query service in a loop

I am checking my database to see if the record exists if it does not exist I run a create service. I am using the following code to

1) get a group of messages from twilio
2) create a local array
3) loop through the array and take action

sms_to_local_To.execute();
var items = Apperyio.storage.messageDBData.get();
for (var i = 0; i < items.length; i++)
{
var currItem = items;
Apperyio.storage.messageDB.set(currItem);
Check_Message_ID.execute();
}

the problem I am having is that the query service goes though all the array but only takes action on the first record. when I use this look to just copy the array directly to the database it works for each record and records them, but for some reason it won't step through the loop one at a time. The mapping on the where is based on the local variable currItem.

Serhii Kulibaba
Posts: 150
Joined: Tue Aug 27, 2013 1:47 pm

Query service in a loop

Hello Jeff,

Please follow this topic: https://getsatisfaction.com/apperyio/...

Jeff Salter
Posts: 0
Joined: Sun Jul 05, 2015 2:30 pm

Query service in a loop

This is what I came up with but it is running both the success and failure on each pass??

function successFunction(data){

console.log('response is success');

}

function errorFunction(data){

console.log("response is error");
}

var messages= Apperyio.storage.messageDBData.get();
var currMsg;
var twilioId;

for (i = 0; i < messages.length; i++) {

Code: Select all

 currMsg = messages[i]; 
 console.log("Message = " + currMsg); 
 Apperyio.storage.messageDB.set(currMsg); 
 twilioId = Apperyio.storage.messageDB.get("$['twilioId']"); 
 console.log("twilioId =" + twilioId); 

 Check_Message_ID.execute({ 
     data:{"where": '{"twilioId":"'+twilioId+'"}'}, 
     success: successFunction(data), 
     error: errorFunction(data) 
 }); 

}

Serhii Kulibaba
Posts: 150
Joined: Tue Aug 27, 2013 1:47 pm

Query service in a loop

Could you clarify what have you tried and what exactly does not work?

I see you use the same value for each service's run:

twilioId = Apperyio.storage.messageDB.get("$['twilioId']");

it can be related with your issue

Jeff Salter
Posts: 0
Joined: Sun Jul 05, 2015 2:30 pm

Query service in a loop

I am trying to record record only messages not yet recorded from twilio. I tried using the advice given above and created the function call on success/error, but it was not working as if the message id is a match then it should error?
I record all messages at twilio to the array, the start the loop. on each pass I record that message to localstorage (MessageDB), I invoke the "Check_Message_ID" as a query service and if the messages is not yet in the local database i want to record it, I have mapped MessageDB to a create service. The issue is that it runs the success function and the error function on each pass, but it should only run one if there is a match(success) or not a match(error). I removed the create service for testing to just show the console log. My log shows it is running each service on each pass. The twilio ID is switching on each pass as expected. I tried using a if data === 0 || if data ===NULL on the success but that did not work either. Something seems to be wrong on the success/failure event, it can't run both.

Jeff Salter
Posts: 0
Joined: Sun Jul 05, 2015 2:30 pm

Query service in a loop

It's been 4 day since I had any response. My loop runs multiple times as expected the TwilioID changes as expected with each loop, then the Check_message _id query runs once for each record, but does so after the other loop processes and uses just the last value of the loop. It always runs the success event and the error event. I have tried multiple iterations of the script and still not working

Serhii Kulibaba
Posts: 150
Joined: Tue Aug 27, 2013 1:47 pm

Query service in a loop

Could you show us value of the codeApperyio&#46;storage&#46;messageDB&#46;get("$['twilioId']");/code variable?

Jeff Salter
Posts: 0
Joined: Sun Jul 05, 2015 2:30 pm

Query service in a loop

Image

As you can see the twilioID changes through the loop, but then the query service runs with the last value only. I need it to run on each loop, then I will put an add record based on the vaue of MessageDB.

Here is my latest iteration of the code:

//this function should run on success of the Check_Message_ID query.
function successFunction(data){
//this checks to see if there is any data and if not runs a record to database event.
// i've tried this code here and in the success event in the query service
if (data === null || data.length ===0){
sms_local_db.execute({});
console.log("twilioIdsuccess = " + twilioId);
}

}

// this grabs the current messages and places them in an array
var messages= Apperyio.storage.messageDBData.get();
var currMsg;
var twilioId;

// this loops for each mesages
for (i = 1; i < messages.length; i++) {

Code: Select all

 currMsg = messages[i];     
 //this grabs the current looped array and places it in a storage variable 
 Apperyio.storage.messageDB.set(currMsg); 
 //this grabs the current value of twilioId to use in query 
 twilioId = Apperyio.storage.messageDB.get("$['twilioId']"); 
 console.log("twilioId = " + twilioId); 
 // this is the query of the appery database that needs to run once on every pass.(problem Here) 
 Check_Message_ID.execute({  

data:{"where": '{"twilioId":"'+twilioId+'"}'},
success: successFunction(data),

});
}

Serhii Kulibaba
Posts: 150
Joined: Tue Aug 27, 2013 1:47 pm

Query service in a loop

Jeff I found solution for your problem. You have to run service with every twilioId on compete event of the previous one.
So, you have to:
1) get twilioId from the storage variable according to it's index (0 for the firs run)

prevar index = Apperyio&#46;storage&#46;index&#46;get();&#47;&#47;datatype = number
var messages = Apperyio&#46;storage&#46;messageDBData&#46;get();
if (index < messages&#46;length){
Check_Message_ID&#46;execute({
data:{"where": '{"twilioId":"'+messages[index]+'"}'},
success: successFunction(data)
});
}/pre

2) on service's complete event increment index variable and save it into storage variable
prevar index = Apperyio&#46;storage&#46;index&#46;get();
Apperyio&#46;storage&#46;index&#46;set(index++);/pre

3) repeat first step

Jeff Salter
Posts: 0
Joined: Sun Jul 05, 2015 2:30 pm

Query service in a loop

I created a variable "index" and made if of a type number.

when I cosole.log index in the complete event it is not increasing.
It still runs the query multiple times with the same twilioId

Can you help me to make sure that I am inserting the code in the right place?

Return to “Issues”