JSbarcode not rendering

1.4k views Asked by At

I am trying to create a route in a nodejs app that generates a barcode from an objectId on a jade template However, as I included my route and refresh, the images is not rendering.

I've read the documentation and tried different methods but it is not rendering still.

Here is my barcode.js

var express = require('express');
var router = express.Router();
var JsBarcode = require('jsbarcode');

router.get('/', function(req, res, next) {
res.render('barcode', { title: 'Barcode' });

JsBarcode("#barcode", "Hi!");


});

module.exports = router;

Here is my jade template:

doctype html
html
    head
        title= title

 svg#barcode

When i refresh the page, nothing shows (blank page)

What i am trying to do is get the image to render first and then eventually add more logic.

1

There are 1 answers

0
Bharath On BEST ANSWER

After res.render you can't have any line of code, since the page would render.

Change your barcode.js file as mentioned below

var express = require('express');
var router = express.Router();

router.get('/', function(req, res, next) {
  res.render('barcode', { title: 'barcode' });
});

module.exports = router;

Change your barcode.jade file as mentioned below

doctype html
html
  head
    title= title
  body
    svg#barcode
    script(src="https://cdn.jsdelivr.net/npm/[email protected]/dist/JsBarcode.all.min.js")
    script.
      JsBarcode("#barcode", "Hi!");

This should give you a barcode on the page. Then you could look into sending ObjectId to the jade file for the new barcode to be generated.