-1

I have created a node module

Module file :

var functions = {};
functions.test = function(){
  console.log("Invoked");
  return "Hello";
}
module.exports = functions;

Main File :

const FUNCTIONS = require('./modulefile');
var x = FUNCTIONS.test();
console.log(x);

Now, here I can see "Invoked" means function is getting executed.

But x is undefined, seems value is not getting returned.

How can I return value from test() to main file.

Drewness
  • 5,004
  • 4
  • 32
  • 50
Atul Sharma
  • 9,397
  • 10
  • 38
  • 65
  • 2
    The question lacks https://stackoverflow.com/help/mcve . `functions.test(){` is syntax error. Real code differs in some way that wasn't shown. – Estus Flask Apr 08 '19 at 18:32
  • This would not work either. You have `tes` but calling `test`. Care to provide working code that demostrates the issue. – Yury Tarabanko Apr 08 '19 at 18:35
  • @YuryTarabanko updated. Its was a typo. The function is getting invoked properly but, not returning the data. – Atul Sharma Apr 08 '19 at 18:36
  • @estus updated. – Atul Sharma Apr 08 '19 at 18:36
  • 1
    MB your actual function. The one you have posted works as expected. So your question basically lacks [mcve] – Yury Tarabanko Apr 08 '19 at 18:37
  • @YuryTarabanko `var x = FUNCTIONS.test();` dosen't returns the value. `x` is getting undefined. While the function `test` in module is returning. – Atul Sharma Apr 08 '19 at 18:38
  • 2
    Not true :) https://repl.it/repls/RobustPrimaryVisitor The problem is some where else. – Yury Tarabanko Apr 08 '19 at 18:40
  • 1
    If the function returned data as shown, there would be no chance for it to not return it. As I mentioned, real code differs in some way. – Estus Flask Apr 08 '19 at 18:46
  • @YuryTarabanko Thanks for help. The problem seems like, i was returning from inside of 2 way nested function and actual return is not happening, which is getting trapped somewhere. Using callback solved the issue. – Atul Sharma Apr 08 '19 at 18:47

2 Answers2

1

You could use callbacks?

Hard to say what the underlying problem is considering people have got your code working.

Model file:

var functions = {
  test: function(callback) {
    console.log("Invoked");
    callback("Hello")
  }
}
module.exports = functions;

Other file:

var Functions= require('./functions');
var x
Functions.test(function (result) { x = result });
console.log(x);
Neil
  • 2,004
  • 3
  • 23
  • 48
  • it worked ! The problem seems, that i was returning from inside of 2 way nested function and actual return is not happening, which is getting trapped somewhere. – Atul Sharma Apr 08 '19 at 18:46
  • If the code is synchronous as shown, the callback is absolutely redundant. – Estus Flask Apr 08 '19 at 18:46
  • 1
    @AtulSharma Then the question is possible duplicate of this popular one, https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call – Estus Flask Apr 08 '19 at 18:50
0

Your code works just fine, I replicated it and it works check it out here https://repl.it/@Muhand1/module-export

Muhand Jumah
  • 1,726
  • 1
  • 11
  • 26