Some problems of a node.js beginner

114 views
Skip to first unread message

Janos

unread,
May 21, 2014, 9:22:55 AM5/21/14
to nod...@googlegroups.com
Hi there,

is there anybody who can help me? I think I have a asynchronous data problem:

My Code ( I made 3 approaches) 


var http = require('http');

var work = require('timetrack');

var mysql = require('mysql');

var qs = require('querystring');

var sync = require('synchronize')


function getonevalue(statement) //approach1

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);

var globalrows = connection.query(statement);

console.log('Statement ausgeführt');

console.log(globalrows[0].username));

//connection.query(statement, function(err, rows, fields) {

  //if (err) throw err;

  //globalrows = rows;

  //console.log('The solution is: ', rows[0].username);

//))});

console.log('returning ', globalrows[0].username));

return globalrows[0].username);

connection.end;


}


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;

});


}



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); 

}

)

connection.end;

};


I called them via e.g.


var myreturn = mysqlhandler.getonevalue3("Select username from tbluser where username = '" + username + "'");

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!


1000 thanks in advance!

Janos



Aria Stewart

unread,
May 21, 2014, 10:27:06 PM5/21/14
to nod...@googlegroups.com

On May 21, 2014, at 9:22 AM, Janos <ja...@sallai.de> 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
signature.asc

Ryan Schmidt

unread,
May 21, 2014, 9:40:03 PM5/21/14
to nod...@googlegroups.com
On May 21, 2014, at 08:22, Janos wrote:

> is there anybody who can help me? I think I have a asynchronous data problem:
>
> My Code ( I made 3 approaches)
>
> var http = require('http');
> var work = require('timetrack');
> var mysql = require('mysql');
> var qs = require('querystring');
> var sync = require('synchronize')
>
> function getonevalue(statement) //approach1

[snip]

> function getonevalue3(statement) //approach2

[snip]

> function getonevalue2 (statement) //approach3

[snip]

> I called them via e.g.
>
> var myreturn = mysqlhandler.getonevalue3("Select username from tbluser where username = '" + username + "'");
> console.log('checkuser returnvalue:');
>
> console.log(myreturn);)

[snip]

> The only thing I want is a synchron function which delivers one value from a mysql db!

This can't work. The database returns the result asynchronously, so you cannot write a function that interacts with it synchronously.

Your function signature needs to be:

function getonevalue(statement, callback)

And you will write that function to call the callback once it has gotten the value from the database. So you will call it like this:

mysqlhandler.getonevalue("Select username from tbluser where username = '" + username + "'", function(err, myreturn) {
if (err) {
console.error(err);
return;
}
console.log(myreturn));
});

And then there are other problems, such as you shouldn't be creating an SQL string by concatenating user-supplied values such as username, but that's a different topic.

mouthful...@gmail.com

unread,
May 27, 2014, 12:39:28 PM5/27/14
to nod...@googlegroups.com
Returning works in mysterious ways :P

Okay, maybe this can help you get a grasp of what you are doing..

Fire up your node REPL.


By executing node without any arguments from the command-line you will be dropped into the REPL.

And try this:

function  test (a, cb) {
console.log("inside test function.. test parameter value is: ", a);
console.log("Back to test again, the value is returned from the callback: ", cb(a+1));
}

test(0, function (b) {
console.log("inside the anonymous function provided as callback to test");
console.log("the value of anonymous parameter is: ", b);
return b+1;
});

The anonymous function returned value only has meaning because the function that called it is expecting a value.
The function that accepts a callback is the one that receives the return value from the callback.

This example was only to clarify who gets your returned value.

Another point: observe that the return statement of the function you pass as a callback to connection.query
is not the return of your main function -code that follows the callback in the outermost braces also gets executed.

Oh, and it doesn't matter (at least regarding the return statement) whether you provide a named or an anonymous function
as a callback. Try this:

copy paste:
function  test (a, cb) {
console.log("inside test function.. test parameter value is: ", a);
console.log("Back to test again, the value is returned from the callback: ", cb(a+1));
}

and then:
function namedFunction (b) {
console.log("inside \'namedFunction\' provided as callback to test function..");
console.log("namedFunction parameter has the value: ", b);
return b+1;
}

and run:
test(0, namedFunction);



On Wednesday, May 21, 2014 4:22:55 PM UTC+3, Janos wrote:
Hi there,


[..]

Reply all
Reply to author
Forward
0 new messages