Getting MySQL result to another function outside the callback function

36 views
Skip to first unread message

Micael Ribeiros

unread,
Jan 16, 2012, 12:36:27 PM1/16/12
to APE Project
This might be a simple question (or not).
Im trying to get the result of a MySQL to be processed on a function,
but as it enters the MySQL callback function I cant access data or
functions on my class probably because it hasnt processed the data yet
so I cant process that data on my function.
Example:
getFriends: function (uid) {
var friendslist = new Array();
res = this.db.query('SELECT friendid FROM friends WHERE userid=1',
function(res, errorNo) {
if (errorNo) {
return ['101', 'MYSQL_ERROR'];
} else {
//Here I can process the result but I cant put the result for
example on friendslist like this:
for(var i = 0; i < res.length; i++) {
friendslist.push(res[i].friendid);
}
}
});
Ape.log(friends.toSource()); //Here the friends Array is still empty
as it was processed in the callback function or because when it
reaches here the callback function hasnt processed the results yet
return friendslist ;
},

Any ideas on how to solve this?
Is there a "good" way to delay the rest of code so I get the results
or some other way of doing it?
Thank you in advance for any response

Mike

unread,
Jan 16, 2012, 12:40:51 PM1/16/12
to APE Project
Put the return inside the callback. Outside return -1.

Micael Ribeiros

unread,
Jan 16, 2012, 12:46:04 PM1/16/12
to APE Project
Ive tried to delay the function like this but it didnt work:

getFriends: function (uid) {
var friendslist = new Array();
res = this.db.query('SELECT friendid FROM friends
WHERE userid=1',
function(res, errorNo) {
if (errorNo) {
return ['101', 'MYSQL_ERROR'];
} else {
//Here I can process the result but I
cant put the result for
example on friendslist like this:
for(var i = 0; i < res.length; i++) {

friendslist.push(res[i].friendid);
}
}
return -1;

Micael Ribeiros

unread,
Jan 16, 2012, 12:53:11 PM1/16/12
to APE Project
"return -1;" instead of "return friendslist;" doesnt work aswell

Mike

unread,
Jan 16, 2012, 1:20:12 PM1/16/12
to APE Project
don't return friendslist; return either using cmd.user.pipe.sendRaw or
cmd.sendResponse

Micael Ribeiros

unread,
Jan 16, 2012, 2:33:53 PM1/16/12
to APE Project
Even if I do that it wont return anything as the array is empty

Mike

unread,
Jan 16, 2012, 3:07:28 PM1/16/12
to APE Project
Outside always returns -1. That means nothing to return. Inside the
loop you do your return. You have to think in terms of the callback.

Micael Ribeiros

unread,
Jan 16, 2012, 3:11:41 PM1/16/12
to APE Project
Yes, but how do I process the data if I can only return it inside the
loop/callback, I cant even pass data to the callback, or can I?

utan

unread,
Jan 24, 2012, 3:49:29 PM1/24/12
to APE Project

Did you solved this issue?

Returning the callback var inside anonymous function and making
available to the parent function?
I am stuck on that..

regards
Message has been deleted

Sándor Volenszki

unread,
May 8, 2015, 3:30:34 AM5/8/15
to ape-p...@googlegroups.com
Hi Utan!

Do you have any solution for this problem? I am also stucked here.

Thanks in advice!

Pablo Tejada

unread,
May 8, 2015, 9:04:28 AM5/8/15
to ape-p...@googlegroups.com

Hey guys,

This a typical scenario in JS, you must call your function in the callback. In the case of APE if you need to get the data back to the user then in your function/callback you must sent a raw to the user pipe, like Mike suggested. The response to the user does not needs to originate from the original request.


--
--
You received this message because you are subscribed to the Google
Groups "APE Project" group.
To post to this group, send email to ape-p...@googlegroups.com
To unsubscribe from this group, send email to
ape-project...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/ape-project?hl=en
---
APE Project (Ajax Push Engine)
Official website : http://www.ape-project.org/
Git Hub : http://github.com/APE-Project/

---
You received this message because you are subscribed to the Google Groups "APE Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ape-project...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Sándor Volenszki

unread,
May 9, 2015, 11:04:12 AM5/9/15
to ape-p...@googlegroups.com

Dear Pablo!


I don't want to be irksome, but I still don't understand, and I feel, I composed my question badly. So I did a snipet to represent my real problem:


Ape.registerCmd('somecommand', false, function(params, infos) {

    var channel = Ape.getChannelByPubid(params.pipe);

    ...

    sql.query("SELECT * FROM table", function(res, errorNo) {

        //Here I get the res, which contains all the mysql returned data

    });

    ...

    //And than I wnat to work with the res outside the sql, so here:

    if(res[0].someparam == 1){

        //Some bussines logic with sql queries and other stuff

    }

    else{

        //Some other bussines logic with sql queries and other stuff

    }

    ...

    if(channel){

        //And use here in the returning data

        channel.pipe.sendRaw('response',{'sqldata':res[0].somedata});

    }

    else{

        return ['109', 'UNKNOWN_PIPE'];

    }


    return 1;

});


How can I do this?


Thank you for your attention!

Pablo Tejada

unread,
May 9, 2015, 12:25:29 PM5/9/15
to ape-p...@googlegroups.com

Hey,

Sorry for the formatting, change your sample in my mobile. Try something like this:

Ape.registerCmd('somecommand', false, function(params, infos) {
   var channel = Ape.getChannelByPubid(params.pipe);
   ...
   sql.query("SELECT * FROM table", function(res, errorNo) {

//And than I wnat to work with the res outside the sql, so here:


   if(res[0].someparam == 1){
       //Some bussines logic with sql queries and other stuff
   }
   else{
       //Some other bussines logic with sql queries and other stuff
   }
   ...
   if(channel){
       //And use here in the returning data
       channel.pipe.sendRaw('response',{'sqldata':res[0].somedata});
   }

   });

   return 1;
});

Sándor Volenszki

unread,
May 11, 2015, 2:21:18 AM5/11/15
to ape-p...@googlegroups.com
Hi Pablo!

Yes, this is exactly what I do now, and this is what I want to change, because this circumstance makes the code hardy maintenance.

First I thought, I will have one retrun point, at the end of the script, but now I see, It wont work.

Is it possible to make functions somehow in the registered command and calling this function instead of every return?

Do you have any experience about this?

RakonDark

unread,
May 11, 2015, 7:09:04 AM5/11/15
to ape-p...@googlegroups.com
But thats what async doing .
Basics of Async ist to walk through function calls , and every call is a exit , cause nothing wait for the answer (cause async)

so how ever you do it , the function your are in runs to the end what every async you call , its done lately .

so you have to take another function and recall the function with that params .


function parent{
 function childasync(
    callback() {
      afterasync(values); // cause you never know when it happend , now you know it :)
    } )
 {
  // nothing should depend on global values cause you never know when they set by async
 }
 function afterasync(values){
     // work with values;
 }

}


i cant trust, what i not know
Reply all
Reply to author
Forward
0 new messages