Node.JS回调范围问题

时间:2018-01-25 22:17:11

标签: javascript node.js callback ecmascript-5 npm-request

我正在使用JavaScript类来编写一个小工具。我不喜欢回调嵌套和嵌套,所以我喜欢将它们分解为单独的函数。我正在尝试从for循环中运行需要回调的请求。问题是,当我将回调分解为单独的函数时,它会失去for ... in循环的范围,并且它不知道“service.url”是什么。

这是我的代码:

Server: ${service.server}

以下是“pollServices”函数/方法运行时出现的错误:

  

C:\用户\ payton.juneau \桌面\我\项目\节点\ com.etouchmenu.tools.crystalBall \公用事业\ net.js:17               的console.log(val cluster = Cluster.builder().addContactPoint("localhost").build() val session = cluster.connect("MyKeySpace") try { val a = session.execute("Select* from users") println(a.all()) //Show as a Row-List //Sample --> [Row[10, Fri Jan 19 04:05:01 MST 2018, 9217], Row[10, Mon Feb 19 04:05:01 MST 2018, 9217], Row[10, Mon Mar 19 04:05:01 MDT 2018, 9217]] /** I have this example for the convertion but do not supports that format **/ case class Sample (Registro: Int,Fecha: String,Valor: String ) val agregado = Sample(999,"Wed May 20 15:19:21 MDT 31063531","982556517") val json= ("Reg_Num:"->agregado.Registro)~("TimeStamp:"->agregado.Fecha) ~ ("Value:"->agregado.Valor) //This is a List val JsonExam = println(compact(render(json))) println ( pretty(render(json)) ) } catch { case e: Exception => println(s"msg=${e.getMessage}") } );                                      ^

     

ReferenceError:未定义服务       在Request._handleResponses [as _callback](C:\ Users \ payton.juneau \ Desktop \ Me \ Projects \ Node \ com.etouchmenu.tools.crystalBall \ utilities \ net.js:17:36)       在Request.self.callback(C:\ Users \ payton.juneau \ Desktop \ Me \ Projects \ Node \ com.etouchmenu.tools.crystalBall \ node_modules \ request \ request.js:186:22)       在Request.emit(events.js:159:13)       在请求。 (C:\ Users \用户payton.juneau \桌面\我\项目\节点\ com.etouchmenu.tools.crystalBall \ node_modules \请求\ request.js:1163:10)       在Request.emit(events.js:159:13)       在IncomingMessage。 (C:\ Users \用户payton.juneau \桌面\我\项目\节点\ com.etouchmenu.tools.crystalBall \ node_modules \请求\ request.js:1085:12)       at Object.onceWrapper(events.js:254:19)       在IncomingMessage.emit(events.js:164:20)       at endReadableNT(_stream_readable.js:1062:12)       at process._tickCallback(internal / process / next_tick.js:152:19)

任何帮助都会非常感激,对所有类型的东西都是新的......

1 个答案:

答案 0 :(得分:0)

在类上定义2个方法时,它们各自创建自己的独立范围:

class Example {
  method1() {
    // scope of method 1 only visible to method 1
    let hiddenInM1 = 'example'; // <-- only available here
  }
  method2() {
    // scope of method 2 only visible to method 2
    // I don't know what m1 is
  }
}

这些方法有3种方法可以相互共享值。

1。使用外部作用域中可用的变量,其中包含以下两种方法:

let sharedGlobal = 'example';
class Example {
  method1() {
    // I can see sharedGlobal
  }
  method2() {
    // I also can see sharedGlobal
  }
}

这通常是不明智的,因为全球状态是prone to bugs

2。通过该类的上下文,这些方法属于(通过this

class Example {
  constructor() {
    this.sharedClassVar = 'example'
  }
  method1() {
    // I can see this.sharedClassVar
  }
  method2() {
    // I also can see this.sharedClassVar
  }
}

3。通过相互传递参数。

class Example {
  method1(fromMethod2) {
    // I can receive stuff from method2
  }
  method2() {
    this.method1('example')
  }
}

如果您查看代码,则不会出现这些模式,因此_handleResponses无法访问service中定义的pollServices

您可以做出的最简单的更改是自己传递service

_handleResponses(service, e, r, b) {
  //             ^^^^^^^ receive the service here
}

pollServices() {
  for (let service of DATA) {
    Request(service.url, (...args) => this._handleResponses(service, ...args))
    //                                                      ^^^^^^^ pass the service with the args
  }
}
相关问题