简体   繁体   English

从Nodejs模块接收数据。

[英]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. 现在,在这里我可以看到“已Invoked ”表示函数正在执行。

But x is undefined , seems value is not getting returned. 但是x is undefined ,似乎值没有被返回。

How can I return value from test() to main file. 如何将值从test()返回到主文件。

You could use callbacks ? 您可以使用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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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