[英]calling nodejs function from the same file and from the different file
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())
從問題本身還不清楚更新方法將執行什么操作以及對哪些數據進行操作,因此如果您提供真實的代碼,則可能會調整答案。
您必須先定義函數,然后像這樣導出函數-
var update=function(tag,view,date){ ...... } module.exports={ update:update }
這樣,您可以從文件的內部和外部訪問'update()'函數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.