0

I'm sening an id to a module checking if the message hjas been sent or not. After it has not been sent I 'm sending the message:

 var l = require('./control.js')
 sonuc = l.check(id);

And my module is

 exports.check = function(id, callback){
        try{
          client.query("SELECT req FROM messages where req = 0 and id = "+id,
          function (err, results, fields) {
            if (results.length > 0){
                callback(false);
                }else{
                callback(true);
                }
         });
         callback();
  }catch(err){
    console.log(err);
  }  
  } 

But it's failing and returning undefined variable.

1 Answer 1

2

returning from an asynchronous function doesn't mean anything; the whole point of using a callback is so you can get the value at a later date:

var l = require('./control.js')
l.check(id, function(sonuc) {
  // Here the value of sonuc is set.
});

You may be interested in this screencast, which goes over the topic in a bit more detail.

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

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.