Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript function returning undefined value in nodejs

I am writing code for getting data. First I call **getsomedata** function to get data and inside getsomedata function I am calling another function getRandomdata to get data and returning it back to the previous function but it is returning undefined. But in getRandomdata data could be seen in console.log. Do I need to use callbacks ?

router.get('/get-data', function (req, res, next) {
    var result = getsomedata(some_parameter);
    console.log(result);   // receiving undefined
    res.send(result);
});

function getsomedata(some_parameter_recieved) {
    var getsomedata = getRandomdata(random_params);
    console.log(getsomedata);    // receiving undefined
    return getsomedata;
}

function getRandomdata(random_params_recieved) {
    // after some calculation 
    console.log(random_data);           // receiving proper data
    return random_data;
}
like image 436
iam Avatar asked Feb 10 '16 06:02

iam


People also ask

Why is my function returning undefined JS?

A function returns undefined if a value was not returned . Note: While you can use undefined as an identifier (variable name) in any scope other than the global scope (because undefined is not a reserved word), doing so is a very bad idea that will make your code difficult to maintain and debug.

Why this is undefined in Nodejs?

Undefined: It means the value does not exist in the compiler. It is the global object.

Why is JavaScript parameter undefined?

In JavaScript, a parameter has a default value of undefined. It means that if you don't pass the arguments into the function, its parameters will have the default values of undefined . The say() function takes the message parameter.

Can we return value from JavaScript function?

JavaScript provides for passing one value back to the code that called it after everything in the function that needs to run has finished running. JavaScript passes a value from a function back to the code that called it by using the return statement. The value to be returned is specified in the return.


1 Answers

Instead of return, you should use callbacks because in asynchronous operations, return does not wait for the I/O operation to complete.

Callback - In JavaScript, higher order functions could be passed as parameters in functions. Since JavaSCript is a single threaded, only one operations happens at a time, each operation thats going to happen is queued in single thread. This way, passed functions(as parameter) could be executed when rest of the parent functions operation(async) is completed and script can continue executing while waiting for results.

Usually this callback function is passed in as the last argument in the function.

Using Callbacks:

router.get('/get-data', function(req, res, next) {
  getsomedata(some_parameter, function(result) {
    console.log(result);
    res.send(result);
  });
});

function getsomedata(some_parameter_recieved, callback) {
  getRandomdata(random_params, function(random_data) {
    callback(random_data);
  });
}

function getRandomdata(random_params_recieved, callback) {
  // after some calculation
  callback(random_data);
}

Using Promise:

router.get('/get-data', function(req, res, next) {
  getsomedata(some_parameter, function(result) {
    console.log(result);
    res.send(result);
  });
});

function getsomedata(some_parameter_received, callback) {
  getRandomdata(random_params).then(function(random_data) {
    callback(random_data);
  }).catch(function(e) {
    //handle error here
  });
}

function getRandomdata(random_params_received, callback) {
  return new Promise(function(resolve, reject) {
    // after some calculation
    if (RandomDataGeneratedSuccessfully) {
      resolve(random_data);
    } else {
      reject(reason);
    }
  });
}
like image 191
Rayon Avatar answered Oct 07 '22 18:10

Rayon