首页 > 解决方案 > 错误类型错误:无法读取未定义的属性“handleStopRecording”

问题描述

我正在调用 handleStopRecording() 函数,但出现错误 ERROR TypeError: Cannot read property 'handleStopRecording' of undefined

stream.getTracks().forEach(function (track) {
      track.addEventListener(
        "ended",
        function () {
          callback();
          callback = function () {
            console.log("stop recording")
            this.handleStopRecording()
          };
        },
        false
      );
      track.addEventListener(
        "inactive",
        function () {
          callback();
          callback = function () {
            console.log("end");
          };
        },
        false
      );
    });
  }

标签: angularrecordrtc

解决方案


在您的情况下,查看this的上下文。在函数内部, this 的值取决于函数的调用方式。

您可以在以下位置获取更多信息:https ://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this


推荐阅读