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

Error using Uint8Array: input.charCodeAt is not a function #231

Closed
rhengles opened this issue May 7, 2020 · 2 comments · Fixed by #299
Closed

Error using Uint8Array: input.charCodeAt is not a function #231

rhengles opened this issue May 7, 2020 · 2 comments · Fixed by #299

Comments

@rhengles
Copy link

rhengles commented May 7, 2020

When using a Uint8Array object to create my qrcode:

let data = [{ data: new Uint8Array([1,2,3,4,/*example*/5,6]), mode: 'byte' }]
QRCode.toCanvas(myCanvas, data, myOptions, function (error) {
	if (error) console.error(error);
	else console.log('QRCode drawn');
});

The library fails with the error below.

TypeError: input.charCodeAt is not a function
    at encodeUtf8 (qrcode.js:1417:25)
    at new ByteData (qrcode.js:1469:12)
    at buildSingleSegment (qrcode.js:1967:18)
    at qrcode.js:1992:20
    at Array.reduce (<anonymous>)
    at Object.fromArray (qrcode.js:1988:20)
    at createSymbol (qrcode.js:2393:29)
    at Object.create (qrcode.js:2493:12)
    at renderCanvas (qrcode.js:2708:25)
    at Proxy.fnRenderCode (qrcode.js:37:11)

I traced the error to the function below

// path: /lib/core/byte-data.js
function ByteData (data) {
  this.mode = Mode.BYTE
  this.data = new Uint8Array(encodeUtf8(data)) // <-- error here
}

When I change it according to the code below, it works:

// path: /lib/core/byte-data.js
function ByteData (data) {
  this.mode = Mode.BYTE
  if ('string' === typeof data) data = encodeUtf8(data)
  this.data = new Uint8Array(data)
}
@irvandikky
Copy link

In Node "Buffer instances are also Uint8Array instances", so data.toString() works in this case.

@air2
Copy link

air2 commented Nov 30, 2021

Based on your finding I came to this (ugly) typescript work-around, because modifying the lib in an existing project is of course a little bit problematic :

qr.segments.forEach(segment => {
  if (typeof segment.data !== 'string') {
    segment.data = decodeUtf8(segment.data)
  }
})

soldair added a commit that referenced this issue Jul 13, 2022
fix: binary data not working as described (fixes #289, #231)
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

Successfully merging a pull request may close this issue.

3 participants