Plugin fs.CreateWriteStream

Does bubble have any temp folder to to use fs.createWriteStream in the plugin builder?

Hey,

image

Looks like it works properly.

You also can create and upload files:

http://s3.amazonaws.com/appforest_uf/f1560503091064x127426841973505620/hello_world.txt

1 Like

Thanks @lottemint.md I’ll check it out.

Where are you getting the S3 URL from?

I’ve uploaded the file to the Bubble app.

Is it possible to upload the file within the plugin and return the URL as a value?

Yes, sure.

You can send a file to your Bubble app and then return the URL.

I cant seem to get it to send the API request. It also does not throw any errors

function(properties, context) {
const fetch = require('node-fetch');
const fs = require("fs");
const PDFDocument = require('pdfkit');

let file = fs.createWriteStream('testfile.pdf');    
let doc = new PDFDocument({layout: "portrait", size: [4 * 72, 6 * 72], margins:{top: 10, bottom: 5, left: 5, right: 5}});

	doc.pipe(file);
    //PDF Data Goes here
    doc.end();    
 	
    file.on("finish", function(){
     
     let data = {fileData: fs.createReadStream('testfile.pdf')}
     fetch('https://webhook.site/acf4d875-6544-44b2-9744-c0067f6e5258', { method: 'POST', body: JSON.stringify(data)})
    .then(res => res.json())
    .then(json => console.log(json));
    
    });    

}

Is it a async/await issue?

You need to fix your code. It doesn’t work as should be.
The best way is to test it in the code editor.

I moved the code over to my local machine and fixed it. It works perfectly fine from my machine but when executed on Bubble, nothing happens…

function(properties, context) {

const fetch = require(‘node-fetch’);
const PDFDocument = require(‘pdfkit’);
const fs = require(“fs”);

let file = fs.createWriteStream(“myPDF.pdf”);
let doc = new PDFDocument({layout: “portrait”, size: [4 * 72, 6 * 72], margins:{top: 10, bottom: 5, left: 5, right: 5}});
doc.pipe(file)

// OCODE and DCODE
doc.font(‘Helvetica-Bold’)
doc.fontSize(15)
doc.text(“DXB - DXB”, 140, 22,{width: 143, align: ‘right’});
// Recipient Label
doc.fontSize(10)
doc.text(“RECEIPIENT”, 10, 60, {width: 125, align: ‘left’});
// Recipient Full Name
doc.font(‘Helvetica’)
doc.fontSize(8)
doc.text(“Joe Smith”, {width: 125, align: ‘left’});
// Recipient Phone Number
doc.text("(415) 555-1234", {width: 125, align: ‘left’});
// Recipient Address Description
doc.text(“Mag 5 Boulevard”, {width: 125, align: ‘left’});
// Recipient Area and City
doc.text(“San Francisco, CA 94105”, {width: 125, align: ‘left’});

doc.end();

file.on(‘finish’, function() {
fs.readFile(‘myPDF.pdf’, ‘base64’, function(err, data) {
if(err){console.error(err)};
fetch(‘https://labelsapp.bubbleapps.io/version-test/api/1.1/obj/user/1562434545944x340606601571464600’, {
method: ‘patch’,
headers: {‘Authorization’: ‘Bearer fedb18121f0e13191a8a89b1e0b5b74d’, ‘Content-Type’: ‘application/json’},
body: JSON.stringify({
FILE:{
filename: ‘myLabels.pdf’,
contents: data,
private: false
}
})
})
.catch(err => console.log(err));
});
});

}

1 Like

Try to use Sync functions instead of Async.
Also, use the context.request(options) for making Sync requests.

I tired using async/await but it does not work on Bubble, it works all the time from my local machine. Does bubble not provide a way to see server side errors during the build of plugins?

If I remember correctly, the Bubble doesn’t provide it yet.
They are using Lambda for server-side plugins.

I had experience with Lambda in the past. There is a way to view logs created by console.log() function. They are available in the CloudWatch Logs.

https://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-logging.html

It would be great if @Bubble will provide the possibility to view these logs. It is very hard to configure something without a way to view logs. :pensive:

BTW, you need to create files in the temp directory. For instance ('/tmp/myFile.pdf')

I gave up trying to build this on bubble. I just created it in Lambda and it works perfectly fine.

I’m using only Sync functions for the server-side plugins.

How do you define it to run Sync? I thought the Functions ran in the node.js environment which is all async and using the “await” inside the functions gives a syntax error in bubble editor

fs.readFile > fs.readFileSync

I’m converting Async functions into Sync ones in the case if it doesn’t support Sync.

It works very well!