Silvering
Silvering

Reputation: 787

Node.js/Express Async functions

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!

Upvotes: 1

Views: 1135

Answers (1)

İlker Korkut
İlker Korkut

Reputation: 3250

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);

Upvotes: 2

Related Questions