简体   繁体   中英

Receive data from Nodejs Module.Exports

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.

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

您的代码工作正常,我复制了该代码,并在这里查看了它的代码https://repl.it/@Muhand1/module-export

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM