Returning a object from call back function, so that a wrapping function can return the same object in node.js/express
NickName:Abk Ask DateTime:2014-08-27T00:09:10

Returning a object from call back function, so that a wrapping function can return the same object in node.js/express

I am trying to return an object that I am constructing from data obtained from a call to a third party API. I am using the Request(https://github.com/mikeal/request) module to accomplish the same.

However, I want to return this object from the callback function.

My request call is a part of a javascript function and I want this outer function to be able to return the newly constructed object. I am facing problem due to non-blocking nature of node.js as whenever I try to return object from the outer function, it returns an empty object because the callback function has not yet contructed the object.

How can I achieve this?

function getData(url){

  var myWeatherData = new Object();
  var data;

  request(url, function (error, response, body) {

    if (!error && response.statusCode == 200) {

      data = JSON.parse(body);

      myWeatherData.locationName = data.current_observation.display_location.full;
      myWeatherData.weather = data.current_observation.weather; 
      myWeatherData.temperature_string = data.current_observation.temperature_string;
      myWeatherData.relative_humidity = data.current_observation.relative_humidity;
      myWeatherData.wind_string = data.current_observation.wind_string;
      myWeatherData.feelslike_string = data.current_observation.feelslike_string;

    }
    return myWeatherData; // THIS IS A RETURN FROM CALL BACK 
  });   
// return myWeatherDataArr; -- THIS RETURNS AN EMPTY 
};

Copyright Notice:Content Author:「Abk」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/25510410/returning-a-object-from-call-back-function-so-that-a-wrapping-function-can-retu

More about “Returning a object from call back function, so that a wrapping function can return the same object in node.js/express” related questions

Returning a object from call back function, so that a wrapping function can return the same object in node.js/express

I am trying to return an object that I am constructing from data obtained from a call to a third party API. I am using the Request(https://github.com/mikeal/request) module to accomplish the same.

Show Detail

Returning an Object from middleware function in Node.JS

I am new to Node.JS coming from a Java Background I am using express to build this Rest API . What I am trying to do is build the concept of a manager. I am looking for a elegant way of returning a...

Show Detail

Node.js Express: TypeError: object is not a function

When I run the following code: var app = require('express')(); var http = require('http').Server(app); app.get('/', function(req, res){ res.send('<h1>Hello world</h1&

Show Detail

Node.js .map function causing express server to return the response object early

My goal is to create an abstracted POST function for an express running on Node similar to Django's inbuilt REST methods. As a part of my abstracted POST function I'm checking the database (mongo) ...

Show Detail

node.js express.js object is not a function call_non_function

I have this error: TypeError: object is not a function at Object.CALL_NON_FUNCTION (native) For this line: var app=express(); I tryed to install express/connect again, but.. nothing. Thanks! ...

Show Detail

Return any type of object from a function

Is there any way of returning an object from a function. The tricky part is the object can be of any type. I want a function to handle returning objects from session. So the object i want to return...

Show Detail

express js response returning object as string

Why express js returning object as String. i have array type object const cars = ["Saab", "Volvo", "BMW"]; but when i return this object to calling code its returning...

Show Detail

how to create custom object/array using twilio fetch conference function in Node.js

I have been trying to generate custom object array or JSON object to fetch specific fields of conferences object that Twilio fetch conference API provides. But since Node.js is async so unable to a...

Show Detail

Node.js: object is not a function

I have just started working with Nodejs and trying to create my first node (using express)application. I am getting an error "TypeError: object is not a function" with below message: /home/sears/

Show Detail

Why does express export a function rather than an object at the beginning?

When we use express in Node.js, we first use const express = require('express') to import express module, and this will return a function. Then we use const app = express() Here's my question: W...

Show Detail