Javascript to bubble has no value

Hello fellow bubblers

I added a run on page load workflow to run this JavaScript and added a a Javascript to bubble element, when i inspected the element for a value it shows empty! what am I doing wrong here.

// Dynamically load the buffer library
var script = document.createElement('script');
script.src = "https://cdnjs.cloudflare.com/ajax/libs/buffer/6.0.3/buffer.min.js";

// When the script is loaded, execute the following code
script.onload = function() {
    // Ensure Buffer is defined from the loaded buffer library
    var Buffer = buffer.Buffer;

    // Function to get TLV for a given tag number and value
    function getTLVForValue(tagNum, tagValue) {
        var tagBuf = Buffer.from(tagNum, 'utf8'); // Create buffer for tag number
        var tagValueLenBuf = Buffer.from([tagValue.length]); // Buffer for tag value length
        var tagValueBuf = Buffer.from(tagValue, 'utf8'); // Buffer for tag value
        var bufsArray = [tagBuf, tagValueLenBuf, tagValueBuf]; // Array of buffers
        return Buffer.concat(bufsArray); // Concatenate all buffers into one
    }

    // Function to convert a UTF-8 string to Base64
    function utf8ToBase64(str) {
        return btoa(unescape(encodeURIComponent(str))); // Handle encoding for UTF-8 strings
    }

    // 1. Seller Name
    var sellerNameBuf = getTLVForValue("1", "salah hospital");

    // 2. VAT Registration
    var vatRegistrationNameBuf = getTLVForValue("2", "31234567890123");

    // Placeholder variables for other buffers
    var timeStampBuf = Buffer.from("timestamp", 'utf8');
    var taxTotalNameBuf = Buffer.from("taxTotal", 'utf8');
    var vatTotalBuf = Buffer.from("vatTotal", 'utf8');
    var hashedXmlBuf = Buffer.from("hashedXml", 'utf8');
    var keyBuf = Buffer.from("key", 'utf8');
    var signatureBuf = Buffer.from("signature", 'utf8');

    // Combine all buffers
    var tagsBufsArray = [
        sellerNameBuf, 
        vatRegistrationNameBuf, 
        timeStampBuf, 
        taxTotalNameBuf, 
        vatTotalBuf, 
        hashedXmlBuf, 
        keyBuf, 
        signatureBuf
    ];

    // Concatenate all buffers
    var qrCodeBuf = Buffer.concat(tagsBufsArray);

    // Convert to base64
    var qrCodeB64 = utf8ToBase64(qrCodeBuf.toString('utf8'));

console.log(qrCodeB64);

    // Sending to bubble_fn_1
    bubble_fn_1(qrCodeB64);
};

Any help would be appreciated

1 Like

I like code so Iā€™ll take a shot at this.

It seems like the script is trying to run before the library is loaded?

I see you have an event handler, but I think you could set it up better.

I would try this:

function loadBufferLibrary() {
return new Promise((resolve, reject) => {
var script = document.createElement(ā€˜scriptā€™);
script.src = ā€œhttps://cdnjs.cloudflare.com/ajax/libs/buffer/6.0.3/buffer.min.jsā€;
script.onload = resolve;
script.onerror = reject;
document.head.appendChild(script);
});
}

// Run on page load workflow
loadBufferLibrary()
.then(() => {
// Code using Buffer goes here
var Buffer = buffer.Buffer;

// Your existing logic for generating the QR code using Buffer

// Sending to bubble_fn_1
bubble_fn_1(qrCodeB64);

})
.catch(error => {
console.error(ā€œError loading buffer library:ā€, error);
});

Itā€™s called a ā€˜promise based approachā€™

Iā€™m a little lazy sometimes so I used Gemini to correct the code.

If that doesnā€™t work, I would guess itā€™s how youā€™re generating your code for the qr code.

I would try the code I gave. Itā€™s hard sometimes to diagnose a problem without having a lot of other detailsā€¦but itā€™s my best shot for what little Iā€™ve read.

Is the console.log giving you the correct result?

Anyway for J2B elements most of the time this issue is a result of settings in your J2B element. Hereā€™s what you can check:

  • Have you checked the ā€œPublish Valueā€ checkbox?
  • Is there any additional space at the beginning or the end of the bubble_fn name value in the element
1 Like