On May 21, 2014, at 9:22 AM, Janos <[email protected]> wrote:

> Hi there,
> 
> is there anybody who can help me? I think I have a asynchronous data problem:
> 
> My Code ( I made 3 approaches) 
> 
> 
>       var globalrows = connection.query(statement);
>       console.log('Statement ausgeführt');
>       console.log(globalrows[0].username));

This is asynchronous — query is going to take a callback, generally, so you’d 
need to give it a function to continue into when the query returns. 
connection.query isn’t going to return a useful result unless you use a library 
using promises — and in that case, it won’t have a [0].username, it’ll need a 
function for later.


> function getonevalue3(statement) //approach2
> {
>       console.log('Im getonevalue');
>       var connection = mysql.createConnection({
>         host:     '127.0.0.1',
>         user:     'root',
>         password: '',
>         database: '****'
>       });
>       connection.connect();
>       debugger;
>       console.log('Running SQL: ' + statement);
>       connection.query(statement, function(err, result)
>       {
>               console.log('Statement ausgeführt');
>               console.log(result.rows[0].username);
>               return result.rows[0].username;

Return to where? The outer function has already run. You’re going to want to 
call a callback with the result — forward, not returning data.

>       });
> 
> }
> 
> 
> function getonevalue2 (statement)  //approach3
>       {
>               var connection = mysql.createConnection({
>                 host:     '127.0.0.1',
>                 user:     'root',
>                 password: '',
>                 database: '****'
>               });
>               connection.connect();
>               console.log('Running SQL: ' + statement);
>                       connection.query
>                               (statement,1, 
>                                       function results(err, rows) 
>                                       {
>                                       if (err) throw err;
>                                       console.log('Gelesener Wert: ');
>                                       console.log(rows[0].username);
>                                       return(rows[0].username); 

Again — use a callback, not return.

>                                       }
>                               )
>               connection.end;
>       };
> 
> I called them via e.g.
> 
> var myreturn = mysqlhandler.getonevalue3("Select username from tbluser where 
> username = '" + username + "'”);

Your function should also take a callback, and have the result there. Return 
values aren’t used much in node.

> console.log('checkuser returnvalue:');
> 
> console.log(myreturn);)
> 
> What I get is:
> 
> Im getonevalue
> Running SQL: Select username from tbluser where username = 'janos'
> checkuser returnvalue:
> undefined
> Statement ausgeführt
> 
> The only thing I want is a synchron function which delivers one value from a 
> mysql db!

Node doesn’t have synchronous primitives for anything but the filesystem. Time 
to learn to use async!

Aria

Attachment: signature.asc
Description: Message signed with OpenPGP using GPGMail

Reply via email to