Decoding base64 string returns undefined

Enonic version: 6.14.x
OS: Mac OS 10.13.6


I want to decode the jwt token by using the base64 decode function from the lib-text-encoding library, but it returns “undefined”. I checked the base64 string here:, and it decodes it fine there.

This is the base64 string i try to decode:

This is how I try to decode it:

var base64 = "eyJodHRwcz.... " // same as the text above
var encodingLib = require('/lib/text-encoding');
var decoded = encodingLib.base64Decode(base64);; // prints "undefined" 

Any tips on why I’m getting “undefined” ?

1 Like

I just tried your code in a main.js of an app to test it, and it works for me.

It prints "ByteSource.wrap(7B2268747470733A2F2F6170692...)" which is the toString() output of the binary object you get back.

What is strange is that if the value is not a valid base64 string, what it returns and prints is "null" , not "undefined".
Do you have another variable defined as decoded in that file?

1 Like

I guess I lied in the question. I did this to log the output:"%s", JSON.stringify(decoded, null, 2));

And JSON.stringify gave me “undefined”. When logging it without stringifying it gave me the same result as what you got.

Thanks a lot for the up clearance :slight_smile: