0

how to acccess rate out side any variable

        rates.fromBTC(1, currency, function (err, rate) {     
                return rate;
        })
shubham
  • 9
  • 3
  • Welcome to Stack Overflow. It would help if you included more context to your question. – bated Aug 02 '17 at 03:58
  • This is like an asynchronous operation. As such, you cannot return the result and use it outside the callback. Instead, you must use the result inside the callback or in some function you call from within that callback and pass the value to. This is how asynchronous development works in node.js. See the question yours has been marked a duplicate of for a very detailed description of this issue. This is one of the most common duplicate questions related to node.js as everyone starting with node.js has to go through a learning curve to learn how to write async code. – jfriend00 Aug 02 '17 at 05:08

1 Answers1

-1
var rates = require('./rate.js');

rates.fromBTC(1, currency, function (response) {     
        console.log(response) //you will get rate here
})

//in rate.js file
exports.fromBTC = function(num,currency,callback){
  /*
  do some operation to find rate
  */
  return callback
}
raj peer
  • 694
  • 6
  • 13
  • This is one of the most common duplicate questions asked related to node.js so we don't need yet another answer. Plus your answer does not seem to explain or solve the problem either. – jfriend00 Aug 02 '17 at 05:05
  • thanks sir but i am new in node js and i want 'rates' for all 'currency' and send response only one ' list' . i have trying and error are occur Error: Can't set headers after they are sent to the client . please give me suggestion what am i do ? – shubham Aug 02 '17 at 08:38
  • app.post('/',function(req,res) { var currency; currency= ["INR","USD"]; currency.forEach(function(value) { console.log("currency : "+value); rates.fromBTC(1, value,function (err, rate) { console.log("getting value for "+ value +" and value is : "+ rate) res.send("currency rates "+rate); });// value+"value is a " }); }); app.listen(3003); console.log("3000 port is running a my app"); – shubham Aug 02 '17 at 08:42
  • what is meaning of – shubham Aug 02 '17 at 10:18
  • this question is marked as duplicate by some one.please refer that question. – raj peer Aug 02 '17 at 11:41