最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - calling nodejs function from the same file and from the different file - Stack Overflow

programmeradmin1浏览0评论

Model.js file has the following entry

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

and calling the function like

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

it throws the following error,

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

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

Model.js file has the following entry

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

and calling the function like

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

it throws the following error,

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

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

Share Improve this question asked Oct 30, 2014 at 6:40 anishanish 7,44814 gold badges84 silver badges153 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

In model.js

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

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

Inside 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.

You have to first define the function, and then export the function like this-

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

module.exports={
  update:update
}

By doing this you can access the 'update()' function from inside as well as outside of the file.

发布评论

评论列表(0)

  1. 暂无评论