从同一个文件和不同的文件调用nodejs函数 [英] calling nodejs function from the same file and from the different file

查看:337
本文介绍了从同一个文件和不同的文件调用nodejs函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Model.js文件包含以下条目

Model.js file has the following entry

exports.update = function(tag,view,date){
{
    .....
    ....
}

并调用函数

 update('test','1213','11/10/2014')

它会抛出以下错误,

update('test','1213','11/10/2014')
^
ReferenceError: update is not defined

我可以从不同的文件调用更新模块,没有任何类似的错误

I can call the update module from the different file, without any error something like this

var model =  require('./Model');
model.update('test','1213','2001/1/23')

问题是如何从相同的js(Model.js)文件中调用Update方法

The question is how can invoke the Update method from the same js (Model.js )file

推荐答案

在模型中。 js

exports.update = function update(tag, view, date) { ... }
exports.update('red', 'colors', new Date())

同时考虑声明一个局部变量if你经常调用这个方法(比如循环)

Also consider declaring a local variable if you call the method often (like in a loop)

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())

从问题本身不清楚更新方法的作用以及哪些数据,如果您提供真实的代码,答案可能会被调整。

Is not clear from the question itself what the update method does and on which data, so the answer may be tweaked if you provide real code.

这篇关于从同一个文件和不同的文件调用nodejs函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆