简体   繁体   中英

JSbarcode not rendering

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.

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/jsbarcode@3.8.0/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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM