<!--This code is used to generate a bar code, create an image file and then make the file available for download in the browser-->
bwipjs.loadFont('Inconsolata', 108, require('fs').readFileSync('node_modules/bwip-js/fonts/Inconsolata.otf', 'binary'));
bwipjs.toBuffer({
bcid: 'code128',
text: 'ID:' + result[0]._id + '\nDevice Name:' + result[0].device_name + '\nDevice Model' + result[0].device_model,
scaleX: 0.5,
scaleY: 0.5
}, function(err, png) {
if (err) {
console.log("Error in generating barcode");
console.log(err.stack);
}
fs.writeFile('images/' + device + '.png', png, function(err) {
if (err) {
return console.error(err);
}
});
res.download('images/' + device + '.png', device + 'png', function(err) {
if (err) {
console.log("Error:");
return console.error(err);
}
});
});
Fs.ensure error no error occurs when trying to generate a barcode for the second time in the same session using bwip-js module
339 views Asked by Samin Batra At
1
writeFile
is async. You need to move download part into callback