Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Camera does not stop scanning #944

Open
srikanth716 opened this issue Aug 23, 2024 · 0 comments
Open

Camera does not stop scanning #944

srikanth716 opened this issue Aug 23, 2024 · 0 comments

Comments

@srikanth716
Copy link

When starting the QR code scanner and then attempting to stop the scanning before it completes (e.g., by clicking the back button), the following error occurs:
TypeError: Cannot read properties of undefined (reading 'stop')

Steps to Reproduce:

Initialize QR Scanning:
Call the initiateQrScanning(timer) function to start the QR code scanner.

Attempt to Stop Scanning:
Before the scanning operation completes, call the terminateScanning() function (for example, by navigating away from the page or clicking the back button).

Expected Behavior:
The QR scanner should stop gracefully without throwing any errors, even if it is stopped before the scanning is complete.

Actual Behavior:
The following error is encountered:

TypeError: Cannot read properties of undefined (reading 'stop')
    at terminateScanning (path/to/your/hook/file.js:line:column)
    at path/to/your/component/file.js:line:column
    ...

Code Snippet:

export const useQRScanner = () => {
  const dispatch = useAppDispatch();
  const config = {
    fps: 10,
    disableFlip: false,
    aspectRatio: 1.0,
  };

  let html5QrCode;
  const initiateQrScanning = (timer) => {
    if (!html5QrCode?.getState()) {
      html5QrCode = new Html5Qrcode("reader");
      const qrCodeSuccessCallback = (decodedText) => {
        dispatch(
          verificationInit({
            qrReadResult: { qrData: decodedText, status: "SUCCESS" },
            flow: "SCAN",
          })
        );
        clearTimeout(timer);
        html5QrCode.stop();
      };

      html5QrCode
        .start({ facingMode: "environment" }, config, qrCodeSuccessCallback)
        .catch((e) => {
          console.error("Error occurred:", e.message);
          clearTimeout(timer);
          html5QrCode.stop();
        });
    }
  };

  const terminateScanning = async () => html5QrCode.stop();

  return { initiateQrScanning, terminateScanning };
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant