未定义导出typeerror的node.jscallback

我很难从一个名为InfoHandler.js的单独文件中获取这个函数返回的值,这个文件包含在我的第二个文件中。

module.exports = { getInfo : function (val, callback) { jsonRPC.getInfo(function(json){ Data1 = json.result[2]; Data2 = json.result[0]; Data3 = val; json = JSON.stringify(json); console.log(json); callback(json) }); }, 

我想要这样使用它:

 require("InfoHandler.js"); var Info = InfoHandler.getInfo('50'); 

信息现在应该包含从getInfo函数通过callback准备的jsonstring。 不幸的是我得到:

 TypeError: undefined is not a function 

为callback。

这很可能是一个asynchronousIO问题,有人可以给我一个提示吗?

这不是一个asynchronous的IO问题,你试图调用undefined作为一个函数。 您没有将callback传递给getInfo因此callback undefined 。 相反,做这样的事情:

 InfoHandler.getInfo('50', function(json) { console.log(json); }); 

您需要将callback函数传递给getinfo()

代替:

 var Info = InfoHandler.getInfo('50'); 

尝试:

 InfoHandler.getInfo('50', function(Info) { //Use Info here. });