Node JS - 从同一文件中的另一个方法调用方法 [英] Node JS - Calling a method from another method in same file

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

问题描述

我有这个nodeJS代码。

I have this nodeJS code.

module.exports = {

  foo: function(req, res){
    ...
    this.bar(); // failing
    bar(); // failing
    ...
  },

  bar: function(){
    ...
    ...
  }
}

我需要调用栏()来自 foo()方法内部的方法。我尝试了 this.bar()以及 bar(),但都失败说 TypeError:Object#< Object>没有方法'bar()'

I need to call the bar() method from inside the foo() method. I tried this.bar() as well as bar(), but both fail saying TypeError: Object #<Object> has no method 'bar()'.

如何从另一个方法调用一个方法?

How can I call one method from the other?

推荐答案

这样做:

module.exports = {

  foo: function(req, res){

    bar();

  },
  bar: bar
}

function bar() {
  ...
}

无需关闭。

这篇关于Node JS - 从同一文件中的另一个方法调用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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