Node.js 使用async.series和async.until的嵌套函数调用上的nodejs referenceerror

Node.js 使用async.series和async.until的嵌套函数调用上的nodejs referenceerror,node.js,async.js,Node.js,Async.js,我不熟悉nodejs,试图通过重建现有的i2c传感器系统来学习基础知识。 在一个文件中使用命名函数和async.series使其全部运行。为了保持可重用性,我现在想创建一个类,然后可以导入该类。不幸的是,我犯了一些我不明白的错误 class.js const async = require('async'); const i2c = require('i2c-bus'); class Sensor { constructor (channel) { this.channel = ch

我不熟悉nodejs,试图通过重建现有的i2c传感器系统来学习基础知识。
在一个文件中使用命名函数和async.series使其全部运行。为了保持可重用性,我现在想创建一个类,然后可以导入该类。不幸的是,我犯了一些我不明白的错误

class.js

const async = require('async');
const i2c = require('i2c-bus');
class Sensor {
  constructor (channel) {
    this.channel = channel;
    var self = this;
  }
  openBus (callback) {
    bus = i2c.open(self.channel, (err) => {callback()}); // shorted for stackoverflow
  }
  closeBus (callback) {
    bus.close( (err) => {callback()}); //also shorted for better readability
  }
  connection (callback) {
    /* first variation */
    async.series([openBus, closeBus], callback);
  connection2 (callback) {
    /* second variation */
    async.series([this.openBus, this.closeBus], callback);
  }
}
module.exports = K30;
const i2c   = require('i2c-bus');
const async = require('async');
function Sensor(channel) {
  let that = this; // make 'this' available in sub-function scope
  this.channel = channel;

  function openBus(cb) {
    // open the bus-connection
    bus = i2c.open(that.channel);
  }
  function closeBus(cb) {
    // close the bus-connection
  }
  function connection(cb) {
    async.series([openBus, closeBus], cb);
  }
  function getReading(cb) {
    async.until(
      function() {
        // loop condition e.g. max tries to get reading
      },
      function(cb) {
        connection(cb); // calling nested synchronous connection-routine
      },
      function (err) {
        // result handling
      }
    ); // end async.until
  } // end getReading

  return {
    getReading: getReading
  } // make only 'getReading' available
}
module.exports = {
  Sensor: Sensor
} // make 'Sensor' available
导入类时,我可以毫无问题地创建一个新的传感器“对象”,并使用以下命令直接调用函数:

> var Sensor = require('./class.js');
> var mySensor = new Sensor(1);
> mySensor.openBus(foo);
> mySensor.closeBus(bar);
但如果我尝试调用包装器函数,会出现以下错误:

> mySensor.connection(foo);
ReferenceError: openBus is not defined (at 'connection')
> mySensor.connection2(foo);
ReferenceError: self is not defined (at 'openBus')
我相信这些错误的发生是因为我不理解这个和self的正确用法。遗憾的是,我找不到关于这个话题的好文章。非常感谢您的帮助

更新

前两个Anwser中提供的解决方案实际上是我开始使用“self”之前的第一个方法(在谷歌搜索[这个-那个技巧]之后)。
无论如何,以下是我使用“this.channel”得到的输出/错误:


这不会保存在任何地方
var self=This,因此在函数(构造函数是function)结束时丢失

只需删除构造函数中的上述行,并在任何地方使用
this
而不是
self


确实,
这个
关键字在javascript中有点棘手,但如果您遵循合理的方法,您应该会很好。

您确实对
这个
自我
有问题

类中的每个成员都必须由
this
引用。如果您声明一个名为
var EBZ kriemendt=“SO user”的变量
,要访问它,您需要将它与此一起使用,例如:
console.log(this.EBZ kriemendt)

你需要的是

openBus (callback) {
    bus = i2c.open(this.channel, (err) => {callback()});
  }

然后,
mysensor.connection2(foo)
将很好地工作。

虽然我仍然不完全理解这背后的原因,但我通过去掉“ES6”类定义修复了代码

class.js

const async = require('async');
const i2c = require('i2c-bus');
class Sensor {
  constructor (channel) {
    this.channel = channel;
    var self = this;
  }
  openBus (callback) {
    bus = i2c.open(self.channel, (err) => {callback()}); // shorted for stackoverflow
  }
  closeBus (callback) {
    bus.close( (err) => {callback()}); //also shorted for better readability
  }
  connection (callback) {
    /* first variation */
    async.series([openBus, closeBus], callback);
  connection2 (callback) {
    /* second variation */
    async.series([this.openBus, this.closeBus], callback);
  }
}
module.exports = K30;
const i2c   = require('i2c-bus');
const async = require('async');
function Sensor(channel) {
  let that = this; // make 'this' available in sub-function scope
  this.channel = channel;

  function openBus(cb) {
    // open the bus-connection
    bus = i2c.open(that.channel);
  }
  function closeBus(cb) {
    // close the bus-connection
  }
  function connection(cb) {
    async.series([openBus, closeBus], cb);
  }
  function getReading(cb) {
    async.until(
      function() {
        // loop condition e.g. max tries to get reading
      },
      function(cb) {
        connection(cb); // calling nested synchronous connection-routine
      },
      function (err) {
        // result handling
      }
    ); // end async.until
  } // end getReading

  return {
    getReading: getReading
  } // make only 'getReading' available
}
module.exports = {
  Sensor: Sensor
} // make 'Sensor' available
在'member'-函数中,我现在可以使用'Sensor'的'class'-变量,通过'that'访问它们(例如:'that.channel')

详细信息:

如果我用这个代替那个,它只会在直接调用openBus时工作。在我的示例中,有必要以同步方式调用openBus和closeBus(原因很明显)。由于async.series另外嵌套在async.until中(传感器可能需要多次尝试响应),因此此更改的范围将发生变化。通过使用它,我可以忽略范围

评论:
由于解决方案通常指向在自定义模块中使用嵌套异步调用,因此我将稍微更改初始问题的标题。我仍然希望得到更好的解决方案和/或解释,因此我不会将我自己的anwser标记为已接受。

我通过将“channel”声明为类外的全局变量暂时解决了这个问题。