I am new in nodejs
. Here is my code in nodejs
file.
i want to send data from nodejs
to other javascript
use json.stringify
,but my problem is i get null value...
----------------EDIT-----------------------
my code is
function handler ( req, res ) {
calldb(dr,ke,function(data){
console.log(data); //successfully return value from calldb
});
//i think my problem bellow...
res.write( JSON.stringify(data)); //send data to other but it's null value
res.end('\n');
}
function calldb(Dr,Ke,callback){
// Doing the database query
query = connection.query("select id,user from tabel"),
datachat = []; // this array will contain the result of our db query
query
.on('error', function(err) {
console.log( err );
})
.on('result', function( user ) {
datachat.push( user );
})
.on('end',function(){
if(connectionsArray.length) {
jsonStringx = JSON.stringify( datachat );
callback(jsonStringx); //send result query to handler
}
});
}
How to fix this problem?
I am new in nodejs
. Here is my code in nodejs
file.
i want to send data from nodejs
to other javascript
use json.stringify
,but my problem is i get null value...
----------------EDIT-----------------------
my code is
function handler ( req, res ) {
calldb(dr,ke,function(data){
console.log(data); //successfully return value from calldb
});
//i think my problem bellow...
res.write( JSON.stringify(data)); //send data to other but it's null value
res.end('\n');
}
function calldb(Dr,Ke,callback){
// Doing the database query
query = connection.query("select id,user from tabel"),
datachat = []; // this array will contain the result of our db query
query
.on('error', function(err) {
console.log( err );
})
.on('result', function( user ) {
datachat.push( user );
})
.on('end',function(){
if(connectionsArray.length) {
jsonStringx = JSON.stringify( datachat );
callback(jsonStringx); //send result query to handler
}
});
}
How to fix this problem?
Share Improve this question edited May 4, 2013 at 9:29 ltvie asked May 4, 2013 at 3:44 ltvieltvie 9734 gold badges21 silver badges49 bronze badges2 Answers
Reset to default 6You will need to use callbacks, returning data directly will just return null
because the end
event handler is called later when all the data is ready. Try something like :
function handler ( req, res ) {
calldb(dr, ke, function(data){
console.log(data);
res.write( JSON.stringify(data));
res.end('\n');
});
}
function calldb(Dr,Ke, callback) {
var query = connection.query('SELECT id,userfrom tabel'),
datachat= []; // this array will contain the result of our db query
query
.on('error', function(err) {
console.log( err );
})
.on('result', function( user ) {
datachat.push( user );
})
.on('end',function() {
callback(datachat);
});
}
The problem is that nodejs is asynchronous. It will execute your res.write( JSON.stringify(data)); before your function will be called. You have two options: one to avoid callback:
.on('end',function(){
if(connectionsArray.length) {
jsonStringx = JSON.stringify( datachat );
res.write( JSON.stringify(data));
res.end('\n');
}
}
the other have the response in the callback function like this:
function boxold() {
box(function(data) {
res.write( JSON.stringify(data));
res.end('\n');
//console.log(data);
});
}