qrcodejs icon indicating copy to clipboard operation
qrcodejs copied to clipboard

Cannot read properties of undefined (reading '_android')

Open Cyclodex opened this issue 1 year ago • 8 comments

Hi! 👋

Firstly, thanks for your work on this project! 🙂

Today I used patch-package to patch [email protected] for the project I'm working on.

My issue was, after updating to Vue 2.7.13 and @vue/cli 5.0.8 the generated JS code throws an error, because "this" is undefined when running this line: if (this._android && this._android <= 2.1) {

I think its related to js modules and this is always undefined on global level.

  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this?retiredLocale=de#global_context

image

Here is the diff that solved my problem:

diff --git a/node_modules/qrcode-js-package/qrcode.js b/node_modules/qrcode-js-package/qrcode.js
index f89e602..d5e5bd9 100644
--- a/node_modules/qrcode-js-package/qrcode.js
+++ b/node_modules/qrcode-js-package/qrcode.js
@@ -281,7 +281,7 @@ var QRCode;
 		
 		// Android 2.1 bug workaround
 		// http://code.google.com/p/android/issues/detail?id=5141
-		if (this._android && this._android <= 2.1) {
+		if (this && this._android && this._android <= 2.1) {
 	    	var factor = 1 / window.devicePixelRatio;
 	        var drawImage = CanvasRenderingContext2D.prototype.drawImage; 
 	    	CanvasRenderingContext2D.prototype.drawImage = function (image, sx, sy, sw, sh, dx, dy, dw, dh) {

This issue body was partially generated by patch-package.

Cyclodex avatar Mar 16 '23 05:03 Cyclodex

Have you solved this problem yet?

Sun-hai-yang avatar Mar 21 '23 03:03 Sun-hai-yang

@Sun-hai-yang What do you mean exactly? The problem can be fixed with the above "patch".

Do you need to know how you can apply it to your own codebase?

Cyclodex avatar Mar 21 '23 06:03 Cyclodex

For me it still gives the same issue, even after the fix

IvanVZabrodin avatar Jul 05 '23 21:07 IvanVZabrodin

I have the same problem as you🥲

hotdogc1017 avatar Oct 09 '23 05:10 hotdogc1017

I have found a solution! Replace 'qrcodejs2' with 'qrcodejs2-fix`

in package.json: "qrcodejs2": "^0.0.2", // remove "qrcodejs2-fix": "^0.0.1", // add

hotdogc1017 avatar Oct 09 '23 06:10 hotdogc1017

I have found a solution! Replace 'qrcodejs2' with 'qrcodejs2-fix`

in package.json: "qrcodejs2": "^0.0.2", // remove "qrcodejs2-fix": "^0.0.1", // add

I have checked the source code of the error location and the solution is consistent with @Cyclodex proposed method

hotdogc1017 avatar Oct 09 '23 06:10 hotdogc1017

the same problem

Minori-ty avatar Jan 19 '24 02:01 Minori-ty