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

[BUG]: Creating Buffer in code piece cause "Invalid or Unexpected Token" error #4404

Closed
noahcastor opened this issue Apr 10, 2024 · 5 comments

Comments

@noahcastor
Copy link

noahcastor commented Apr 10, 2024

I've been trying to find a way to upload an image to NocoDB using the code piece for the past 3 days but have failed tremendously. I'm trying to use the QRCode npm package to generate qr codes, but the package returns the image in base64. I was having issues with sending a POST request to the API so I figured it was an issue with the image being in base64.

After some research, I notice some people converting the base64 into a blob, then the blob to a buffer before sending a post request with axios. When I attempt to convert the blob to a buffer, it causes an "Invalid or unexpected token" syntax error. Commenting out the line where I convert the "arrayBuffer" variable to a Buffer removes the error.

CODE:

import QRCode from 'qrcode';
import { Base64 } from 'js-base64';
import axios from 'axios';
import FormData from 'form-data';
import Buffer from 'buffer';



export const code = async (inputs) => {

    const QRCodeBase64 = await QRCode.toDataURL("test"); 
    const QRCodeBinary = await Base64.atob(QRCodeBase64);
    const QRCodeBase64_No_Prefix = QRCodeBase64.replace("data:image/png;base64,", "");
    const QRCodeBinary2 = await Base64.atob(QRCodeBase64_No_Prefix);
    const QRCode_Unit8Array = await Base64.toUint8Array(QRCodeBase64_No_Prefix);

    const blobFile = new Blob([QRCode_Unit8Array], {type: "image/png"});
    const arrayBuffer = await blobFile.arrayBuffer();

    const formData = new FormData();
    formData.append("file", Buffer.from(arrayBuffer));
    // formData.append("mimetype", "image/png");

    // const response = await axios({
    //     url: 'https://nocodb.dexo.digital/api/v2/storage/upload',
    //     data: formData, 
    //     headers:{ 
    //         'Content-Type': 'multipart/form-data',
    //         'xc-auth': 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
    //     },
    //     method: 'post'
    // })
    // console.log(response);
    return true;
};

ERROR:

{"stack":"/usr/src/app/dist/packages/server/api/sandbox/0/codes/step_4/index.js:2\n throw new Error('Compilation Error:\n ^^^^^^^^^^^^^^^^^^^\n\nSyntaxError: Invalid or unexpected token\n at internalCompileFunction (node:internal/vm:73:18)\n at wrapSafe (node:internal/modules/cjs/loader:1274:20)\n at Module._compile (node:internal/modules/cjs/loader:1320:27)\n at Module._extensions..js (node:internal/modules/cjs/loader:1414:10)\n at Module.load (node:internal/modules/cjs/loader:1197:32)\n at Module._load (node:internal/modules/cjs/loader:1013:12)\n at Module.require (node:internal/modules/cjs/loader:1225:19)\n at require (node:internal/modules/helpers:177:18)\n at (webpack://activepieces/engine/src/lib/handler/code-executor.ts:41:65)","message":"Invalid or unexpected token"}

@kishanprmr
Copy link
Collaborator

Hello @noahcastor ,

If you are using API token for NocoDB, then pass token in headers with key 'xc-token'.

@noahcastor noahcastor changed the title [BUG]: Creating Buffer in cod piece cause "Invalid or Unexpected Token" error [BUG]: Creating Buffer in code piece cause "Invalid or Unexpected Token" error Apr 11, 2024
@abuaboud
Copy link
Contributor

@noahcastor
Copy link
Author

noahcastor commented Apr 12, 2024

@kishanprmr

Hello @noahcastor ,

If you are using API token for NocoDB, then pass token in headers with key 'xc-token'.

Thanks for the reply. Looking at their documentation though, it appears that they want us to use the account authorization token with the 'xc-auth' header parameter. The issue I mentioned before though occurs without even making the POST request with axios. It's the line above the first comment line that is causing the issue. I tried to execute Buffer.from() with both the blobFile and the arrayBuffer, but neither worked.

@abuaboud
Copy link
Contributor

Hi @noahcastor

I think it's related to the code snippet and not to activepieces, I am closing this issue as it's not related.

I reopened the topic in https://community.activepieces.com/t/how-do-i-convert-base64-image-to-a-temporary-file/3813

It's more appropriate there.

Thank you,
Mo.

@abuaboud abuaboud closed this as not planned Won't fix, can't repro, duplicate, stale Apr 30, 2024
Copy link

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see.
If this issue is continuing with the latest stable version of Activepieces, please open a new issue that references this one.

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

3 participants