1

I'm playing with node.js and express. I have a little server which fetch sqlite contents and send everything to a Jade template. It works fine using this code :

var express = require('express');
var app = express();
app.set('view engine', 'jade');
var async = require('async');

var result_title = [];
var result_scope = [];
var result_benefits = [];
var result_technical = [];

app.use(express.static(__dirname + '/views'));

app.get('/product1', function(req, res){

  var sqlite3 = require('sqlite3').verbose();
    var db = new sqlite3.Database('products.db');
    var check;
    db.serialize(function() {
        db.each("SELECT title, scope, body, technical_information FROM products", function(err, row) {
          result_title.push(row.title);

            result_scope.push(row.scope);

            result_benefits.push(row.body);

          result_technical.push(row.technical_information);
        });

    });

  console.log(result_title[0]);

    res.render("index", {title:result_title[0], scope:result_scope[0],benefits:result_benefits[0], technical_information:result_technical[0]});


    db.close();

});

app.listen(8080);

My issue is that when I go to page http://localhost/product1:8080 nothing is displayed. A manual refresh of the page is needed to load the content! My research tells me that I need to use Async functions. I edited my code :

var express = require('express');
var app = express();
app.set('view engine', 'jade');
var async = require('async');

var result_title = [];
var result_scope = [];
var result_benefits = [];
var result_technical = [];

app.use(express.static(__dirname + '/views'));

app.get('/product1', function(req, res){

  var sqlite3 = require('sqlite3').verbose();
    var db = new sqlite3.Database('products.db');
    var check;

  async.series([
    function(callback) {
         db.serialize(function() {
             db.each("SELECT title, scope, body, technical_information FROM products", function(err, row) {
               result_title.push(row.title);

                 result_scope.push(row.scope);

                 result_benefits.push(row.body);

               result_technical.push(row.technical_information);
             });

         });
     },
     function(callback) {
       // console.log(result_title[0]);
          res.render("index", {title:result_title[0], scope:result_scope[0],benefits:result_benefits[0], technical_information:result_technical[0]});

        db.close();
      }
    ], function(error, results) {
      console.log('');
    })
});

app.listen(8030);

But the webpage is loading, loading and nothing happens.. I made something wrong, but no idea where for the moment. If someone have an idea it could be great ;-) Thanks!

1 Answer 1

2

Your url is wrong also the second code block your port is different.

Give port name after domain or ip address , if not the request will go /product1:8080 and you haven't any router like that , so request goes to error page also it semes you haven't any error handling for 404.

Try : http://localhost:8080/product1 or http://localhost:8030/product1

Also you have an issue in your second code block :

res.render("index", {title:result_title[0], scope:result_scope[0],benefits:result_benefits[0], technical_information:result_technical[0]});

This line should be executed in all series callback, If not you will not get data you want. Because it's still in async function.

], function(error, results) {
   res.render("index", {title:result_title[0], scope:result_scope[0],benefits:result_benefits[0], technical_information:result_technical[0]});
})

I have just investigated sqllite3 , you don't need to use async library as an extra in this situation(BTW in async functins you have to call callback with return parameter on it). In sqllite3 documentation db.each.

That latest code should work. Try following.

var express = require('express');
var app = express();
app.set('view engine', 'jade');
var async = require('async');

var result_title = [];
var result_scope = [];
var result_benefits = [];
var result_technical = [];

app.use(express.static(__dirname + '/views'));

app.get('/product1', function(req, res){

  var sqlite3 = require('sqlite3').verbose();
    var db = new sqlite3.Database('products.db');
    var check;

  db.serialize(function() {
     db.each("SELECT title, scope, body, technical_information FROM products", function(err, row) {
       result_title.push(row.title);

         result_scope.push(row.scope);

         result_benefits.push(row.body);

       result_technical.push(row.technical_information);
     },function(err, rows){
        if(err){
            // handle error
        }
        res.render("index", {title:result_title[0], scope:result_scope[0],benefits:result_benefits[0], technical_information:result_technical[0]});
     });
    });
});

app.listen(8080);
Sign up to request clarification or add additional context in comments.

5 Comments

Yes I know but this isn't the issue, I updated my code to 8080 and same problem.
Thanks for your help. However same issue : gist.github.com/anonymous/6fe238a282320f9707ea
@Silvering, there is an async issue in your code normally, you have to call callback function inside your async functions but you are looping in rows with sqllite3 library, I investigated it provides a complete callback function so you will be able to render after completion each loop.
Thanks A LOT !! It works like a charm! Thanks again!
@Silvering , Good luck also I added sqllite3 doc link. BTW look async api doc also, I guess you will need next phases, also you can use promises instead of async library to prevent nested code blocks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.