我正在使用 module.exports 导出模块列表。下面是我的 模型/Message.js
var msgModel = function()
{
var getMsg = function() {
return "Hello World";
}
return{
getMsg : getMsg
}
}
module.exports = msgModel;
下面是我的app.js
var msgModel = require('./Models/Message');
console.log(msgModel.getMsg());
它引发了我的错误
console.log(msgModel.getMsg());
^
我无法找出问题所在。
最佳答案
您需要执行console.log(msgModel().getMsg());
。这是因为 msgModel 是一个函数。我建议像下面的示例一样重写您的 msgModel 以实现您想要的调用。
var msgModel = {
getMsg: function() {
return "Hello World";
}
};
module.exports = msgModel;
关于node.js - Nodejs 导出函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42771250/