简体   繁体   English

为什么我有一个错误“TypeError: Cannot read properties of null (reading 'getTracks')”,但代码可以工作?

[英]Why do I have an error "TypeError: Cannot read properties of null (reading 'getTracks')", but the code is working?

Here is my code :这是我的代码:

 const videoElem = document.getElementById("videoScan"); var stream = document.getElementById("videoScan").srcObject; var tracks = stream.getTracks(); tracks.forEach(function (track) { track.stop(); }); videoElem.srcObject = null;

I am trying to stop all camera streams in order to make ZXing work.我正在尝试停止所有摄像机流以使 ZXing 正常工作。 However, I have the following error ( only on android tablet ) :但是,我有以下错误(仅在 android 平板电脑上):

在此处输入图像描述

Problem is, I can't solve it, but everything is working well.问题是,我无法解决它,但一切都运行良好。 And when I do a try/catch, code stops working.当我尝试/捕获时,代码停止工作。 I can't figure out why this error is displaying.我无法弄清楚为什么会显示此错误。

When you say " everything is working well ", you mean that you get what you want as output?当您说“一切正常”时,您的意思是您得到了您想要的输出?

If the code throwing the error is the same you are showing us, then it means that streams contains null .如果抛出错误的代码与您向我们展示的代码相同,则表示streams包含null

Since streams is a const value (thus once only) assigned on the previous instructions, it means that document.getElementById("videoScan").srcObject is null too.由于streams是在前面的指令中分配的const值(因此只有一次),这意味着document.getElementById("videoScan").srcObject也为空。

I'd suggest you to do some other debug by, ie, printing out to JS console (if you have access to it from your Android debug environment, I'm sure there's a way) what's inside stream , before trying to access its getTracks method reference.我建议你做一些其他调试,即打印到 JS 控制台(如果你可以从你的 Android 调试环境访问它,我敢肯定有办法)在尝试访问它的getTracks之前, stream里面有什么方法参考。

Please do also check the compatibility with srcObject with your Android (browser?) environment: MDN has a compatibility list you can inspect.请同时检查srcObject与您的 Android(浏览器?)环境的兼容性:MDN 有一个您可以检查的兼容性列表

The simplest way to catch this null reference error, is to check if tracks actually exists.捕获此空引用错误的最简单方法是检查tracks是否确实存在。

const videoElem = document.getElementById("videoScan");
const stream = videoElem.srcObject;

if (stream && stream?.getTracks()) {
  const tracks = stream.getTracks();
  tracks.forEach(function (track) {
    track.stop();
  });
} else {
  console.warn('Stream object is not available');
}

videoElem.srcObject = null;

So I found the solution.所以我找到了解决方案。 The error stopped the code from continuing, which, in my case, actually made it work.该错误阻止了代码继续运行,就我而言,这实际上使它工作。 I made a very basic mistake : not paying enough attention to the debug results.我犯了一个非常基本的错误:没有对调试结果给予足够的重视。 However, if anyone has the error " Uncaught (in promise) DOMException: Could not start video source " with ZXing on Android, you simply have to cut all video streams to allow chrome starting an other camera.但是,如果有人在 Android 上使用 ZXing 出现错误“ Uncaught (in promise) DOMException: Could not start video source ”,您只需剪切所有视频流以允许 chrome 启动其他相机。 It will not work otherwise.否则它将无法正常工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 代码运行正常,但为什么我在控制台出现错误,Uncaught TypeError: Cannot read properties of null (reading 'style')? - Code is working and runs fine, but why I'm getting error at console , Uncaught TypeError: Cannot read properties of null (reading 'style')? 为什么我不断收到此错误? 未捕获的类型错误:无法读取 null 的属性(读取“术语”) - why do i keep getting this error? Uncaught TypeError: Cannot read properties of null (reading 'term') 未捕获的类型错误:无法读取 null 的属性“getTracks” - Uncaught TypeError: Cannot read property 'getTracks' of null typeerror cannot read properties of null reading uid,我没有异步代码和登录用户有什么想法? - typeerror cannot read properties of null reading uid, I have no asynchronous code and the users logged in any ideas? 为什么这段代码给了我“未捕获的类型错误:无法读取 null 的属性(读取 'push')”错误? - Why this code gives me "Uncaught TypeError: Cannot read properties of null (reading 'push')" error? 为什么 React 中的代码可以运行,但几周后就出错了? 类型错误:无法读取未定义的属性(读取“切片”) - Why the code in react was working, but few weeks later it has error? TypeError: Cannot read properties of undefined (reading 'slice') 未捕获的类型错误:无法读取 null 的属性(读取“addEventListener”)。 该怎么办? - Uncaught TypeError: Cannot read properties of null (reading 'addEventListener'). What to do? 未捕获的类型错误:无法读取 null 的属性(读取“querySelectorAll”)VS 代码 - Uncaught TypeError: Cannot read properties of null (reading 'querySelectorAll') VS Code Javascript / EJS 代码显示错误:类型错误:无法读取 null 的属性(读取“名称”) - Javascript / EJS Code showing error : TypeError: Cannot read properties of null (reading 'name') 错误类型错误:无法读取 null 的属性(读取“toISOString”) - ERROR TypeError: Cannot read properties of null (reading 'toISOString')
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM