0
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.

Ashu
  • 2,066
  • 3
  • 19
  • 33

3 Answers3

0

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

  }
}
Naqeeb Sial
  • 657
  • 6
  • 11
0

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.

galkin
  • 5,264
  • 3
  • 34
  • 51
  • I can't understand why you are using sleep while it is callback function. – Naqeeb Sial May 08 '16 at 11:37
  • see https://www.npmjs.com/package/deasync. It will pause node process becasue sleep is now sync. However, i would never recommend that sort of things. –  May 08 '16 at 11:40
  • Why we need to pause a code? While we can do this by just placing our processing code inside get function – Naqeeb Sial May 08 '16 at 11:45
  • because he would like ot return, it s a sync call, not async. –  May 08 '16 at 11:50
  • I am not much aware of Nodejs. What should be the best practice to implement this sort of scenario. My basic requirement is to get the output from this rest call and need to do some manipulation on that before showing it on screen. – Ashu May 08 '16 at 14:29
  • I think you must learn about async and sync function.It will really help you – Naqeeb Sial May 08 '16 at 17:53
0

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.