0

I am getting error while creating blob type data using Node.js. I am explaining my code below.

saveFiles = async (res, outputJSON, testBedJson) => {
    const encodedOutput = btoa(JSON.stringify(outputJSON));
    let preName = 'intent_based';
    let fullFileName = `${preName}_${moment().format("hh_mm_ss-DD-MM-YYYY")}.ubot`;
    let fullFileTestBedJsonName = `test-bed_${moment().format("hh_mm_ss-DD-MM-YYYY")}.json`;
    const doc = new YAML.Document();
    doc.contents = testBedJson;
    let fullFileTestBedYmlName = `test-bed_${moment().format("hh_mm_ss-DD-MM-YYYY")}.yaml`;

    let ubotFileBlobContent = new Blob([encodedOutput], {type : 'text/plain'});
    let jsonFileBlobContent = new Blob([JSON.stringify(testBedJson, null, 4)], {type : 'application/json'});
    let yamlFileBlobContent = new Blob([doc.toString()], {type : 'text/yaml'});
    responseObj = {
        status: 'success',
        msg: 'Successfully saved the files',
        body: {
            allFiles:[
                {
                    "name": fullFileName,
                    "content": ubotFileBlobContent
                },
                {
                    "name": fullFileTestBedJsonName,
                    "content": jsonFileBlobContent
                },
                {
                    "name": fullFileTestBedYmlName,
                    "content": yamlFileBlobContent
                }
            ]
        }
    }
    res.send(responseObj);
 }

So here I am trying to convert to blob type data and returning to UI for download. But as per my code I am receiving the following error.

Error:: ReferenceError: Blob is not defined
    at saveFiles (/Users/subhrajp/ubot-git/uBot/python3.7-alpine3.8/app/mean-stack/node-js/controller/usecaseWorkflowCtrl.js:2019:31)
    at Object.downloadUbotIntentBased (/Users/subhrajp/ubot-git/uBot/python3.7-alpine3.8/app/mean-stack/node-js/controller/usecaseWorkflowCtrl.js:1326:23)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
    at async /Users/subhrajp/ubot-git/uBot/python3.7-alpine3.8/app/mean-stack/node-js/app.js:262:27

Here I need to convert to blob type data instead of writing into file. Please can anybody suggest how to resolve this issue using Node.js.

2
  • Does this answer your question stackoverflow.com/questions/53637644/… Commented Jul 7, 2021 at 9:52
  • I have cheched this link but did not get much hints. Commented Jul 7, 2021 at 9:57

1 Answer 1

0

Hopefully the poster solved this problem, but for anyone else facing this issue you can access a Blob class in Node by importing the Buffer module. When working with a list as in this case, just stringify your target and plug it into a Blob created with the new keyword. You can then access several Blob methods. Doing this will silence this particular error. Here is a link to the Node Buffer documentation that shows how to use the Blob class: https://nodejs.org/api/buffer.html#buffer

Below is example code that uses Node to get a blob from a list and shows its size in bytes:

const { Blob } = require('node:buffer'); 

const testList = ['foo', 'bar', 'baz'];
const testListJSON = JSON.stringify(testList);

// specifying type is optional
const blob = new Blob([testListJSON], {
    type: "text/plain",
   });
const blobBytes = blob.size;

console.log("List size in bytes: ", blobBytes);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.