Model.js 文件具有以下条目
exports.update = function(tag,view,date){
{
.....
....
}
然后像这样调用函数
update('test','1213','11/10/2014')
它抛出以下错误,
update('test','1213','11/10/2014')
^
ReferenceError: update is not defined
我可以从不同的文件中调用更新模块,不会出现类似这样的错误
var model = require('./Model');
model.update('test','1213','2001/1/23')
问题是如何从同一个 js (Model.js) 文件中调用 Update 方法
最佳答案
在 model.js 中
exports.update = function update(tag, view, date) { ... }
exports.update('red', 'colors', new Date())
如果您经常调用该方法(例如在循环中),也可以考虑声明一个局部变量
var update = exports.update = function update(tag, view, date) { ... }
update('red', 'colors', new Date())
在 foo.js 内部
var update = require('./model').update
update('yellow', 'colors', new Date())
问题本身并不清楚更新方法的作用和数据,因此如果您提供真实代码,答案可能会有所调整。
关于javascript - 从同一个文件和不同文件调用nodejs函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26646600/