var Client = require('node-rest-client').Client;
var client = new Client();
module.exports = {
getWeatherStatus: function() {
        var messageData =  "";
        client.get("http://api.openweathermap.org/data/2.5/weather?q=Pune&appid=123234234234243242", function (data, response) {
            console.log(JSON.parse(data));
            messageData=data;
        });
        //how to set the response of that rest call to this messageData object
        return messageData;
    }
}

this method getWeatherStatus should return the rest response in json format.

Open for totally different suggestion to implement this kind of scenario. My basic requirement is to use this REST call response and send to other functions.

3

There are 3 best solutions below

0
On

I think you are facing difficulties to deal with with callbacks,

In the method getWeatherStatus, instead of returning the result, you should pass it to a callback function once the treatment is done.

If really you are required to return, galk.in answer seems to be a possible way.

Otherwise, check this out,

var Client = require('node-rest-client').Client;
var client = new Client();
module.exports = {
  getWeatherStatus: function(then) {
    var messageData =  "";
    client.get("/some/url", function (data, response) {
        then(err=null, JSON.parse(data));
    });
  }
}

So you may call getWeatherStatus in such way,

// Somewhere else in your code
getWeatherStatus(function fnCallback(err, jsonData) {
  console.log(jsonData) // process data here
})

As suggested, Promise are also a good alternative. but it is still async.

0
On

Since get is callback function so you have to put your return messageData in stead of messageData=data.

Code should be like

 var Client = require('node-rest-client').Client;
var client = new Client();
module.exports = {
  getWeatherStatus: function() {
    var messageData =  "";
    client.get("http://api.openweathermap.org/data/2.5/weather?q=Pune&appid=123234234234243242", function (data, response) {
        return  JSON.parse(data);
    });

  }
}
6
On

In getWeatherStatus you use async function client.get. You need wait result from async function and only than return messageData. For this you can use deasync. Example:

var Client = require('node-rest-client').Client;
var client = new Client();
module.exports = {
  getWeatherStatus: function() {
    var messageData =  "";
    client.get("http://api.openweathermap.org/data/2.5/weather?q=Pune&appid=123234234234243242", function (data, response) {
      console.log(JSON.parse(data));
      messageData=data;
    });
    //We waiting for data.
    while (messageData === "") {
      require('deasync').sleep(10);
    }
    return messageData;
  }
}

But, maybe, you should return Promise, not data.