Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

299
Views
node express sqlite - Passing a variable to Pug view

I am trying to pass a SQLite request stored into a variable to a pug template. My program works fine if using a string value passed to my pug view. But i always get an empty or undefined variable in my html when using the stored request result. I ve been reading many posts and tried multiple explanations but i still cant figure out what is wrong with my code.

Here is my server code

router.get('/mydata', function(req, res){   
    
    var sqlite3 = require('sqlite3').verbose();
    var mydb = new sqlite3.Database('mydb');
    var data = {};
    
    mydb.serialize(function() {
    var rowset = mydb.each("SELECT * FROM mytable", function(err, row) {
        data[row.id] = {
        name: row.name,
        };
        res.render('mydata', { 
            data: data
			});
		});
	});
});

here is my pug code

script.  
    var data = data
 

Precision : object data is fine when i print it on the server side.

Thanks a lot for any help (and sorry if it s an asynch beginer issue...)

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

// db.js
var sqlite3 = require('sqlite3').verbose();
var db = new sqlite3.Database('mydb');
...
module.exports = db;

// routers
var db = require('db');

router.get('/mydata', function(req, res, next) { // add error handler
    db.all('select * from mytable', function(err, rows) {
        if (err)
            return next(err);

        var data = {};
        rows.forEach(function(row) {
            data[row.id] = row.name
        });

        res.render('mydata', {data}); 
    })
});
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!