2

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?

2 Answers 2

6

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

}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks..i follow your code,but i get error 'string is not function'..how to fix this?
my problem was fix,but i get null value again when i send value in other function...see edit (i change my code)
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.