Javascript API Service FileReader is not defined

I’m getting a FileReader is not defined at /usr/local/lib/node_modules exception when attempting to call Backendless.File.saveFile. Only have Backendless SDK package in package.json

Hello @jp19344

Could you please provide code sample to reproduce the problem?

Regarda, Vlad

Blockquote
const byteArray = new Blob( [‘test’] );
Backendless.Files.saveFile(‘test’, ‘test.txt’, byteArray, true).then(
(res) => {
console.log(res)
}).catch((err) => {
console.log(err)
});

Here’s an extremely basic snippet of code that errors out when called.

I’m using the saveFile API with arrayBuffers.
Try this conversion: Blob.arrayBuffer() - Web APIs | MDN

Might help …

Thank you for the provided code sample, but Blob is not supported in the Nodejs environment where the Bussiness Logic is running.

Are you sure you are not getting the Blob is not defined error?

I’ve added a library that implements the Blob standard. I’m not sure if this is why I’m not getting a Blob error, but this is the error I receive in the real time logging window.

Blockquote
UnhandledPromiseRejectionWarning: ReferenceError: FileReader is not defined at /usr/local/lib/node_modules/backendless-coderunner/node_modules/backendless/lib/files/utils.js:52:30 at new Promise () at _callee$ (/usr/local/lib/node_modules/backendless-coderunner/node_modules/backendless/lib/files/utils.js:51:48) at tryCatch (/usr/local/lib/node_modules/backendless-coderunner/node_modules/regenerator-runtime/runtime.js:63:40) at Generator.invoke [as _invoke] (/usr/local/lib/node_modules/backendless-coderunner/node_modules/regenerator-runtime/runtime.js:293:22) at Generator.next (/usr/local/lib/node_modules/backendless-coderunner/node_modules/regenerator-runtime/runtime.js:118:21) at asyncGeneratorStep (/usr/local/lib/node_modules/backendless-coderunner/node_modules/@babel/runtime/helpers/asyncToGenerator.js:3:24) at _next (/usr/local/lib/node_modules/backendless-coderunner/node_modules/@babel/runtime/helpers/asyncToGenerator.js:25:9) at /usr/local/lib/node_modules/backendless-coderunner/node_modules/@babel/runtime/helpers/asyncToGenerator.js:32:7 at new Promise ()
10:32:02 | SERVER_CODE | ERROR | [12678] (node:12678) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag --unhandled-rejections=strict (see Command-line API | Node.js v21.5.0 Documentation). (rejection id: 2)
10:32:02 | SERVER_CODE | ERROR | [12678] (node:12678) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Seems like the Blob works just fine and the error is being thrown in the Backendless API.

Yes, that’s the problem.
In the JS-SDK there is a check on which environment it’s running (browser/nodejs) and if the Blob class is presented in the global scope the SDK assumes it’s nodejs env, however, there is no FileReader class, that’s why you get such error.

In order to provide the right way could you please describe your end goal?

For instance, if you need just to write text content you can pass the text without any transformation.

Backendless.Files.saveFile(‘test’, ‘test.txt’, 'my text content', true).

I’m trying to save an image file in base64.

alright, try to decode it to buffer first Base64 Encoding and Decoding in Node.js and then pass it as content

const yourBuffer = ...

Backendless.Files.saveFile(‘test’, ‘test.txt’, yourBuffer, true).