2015-10-06 90 views
0

我正在玩node.js和express。我有一個小服務器,它獲取sqlite內容並將所有內容發送到Jade模板。它工作正常使用此代碼:Node.js/Express異步函數

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

我的問題是,當我去到頁面顯示http://localhost/product1:8080什麼。需要手動刷新頁面才能加載內容!我的研究告訴我,我需要使用異步功能。我編輯我的代碼:

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

但頁面加載,加載並沒有任何反應.. 我做了一些錯誤,但不知道那裏的時刻。如果有人有一個想法,它可能是偉大的;-)謝謝!

回答

1

您的網址也是錯誤的第二個代碼塊您的端口是不同的。

給域名或IP地址後的端口名稱,如果不是,請求將會去/ product1:8080,並且你沒有任何類似的路由器,所以請求轉到錯誤頁面,它也沒有任何錯誤處理404.

嘗試:http://localhost:8080/product1http://localhost:8030/product1

你也有你的第二個代碼塊的問題:

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

此行應在所有系列的回調來執行,如果沒有你不會得到數據你要。因爲它仍然在異步功能。

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

只是我已經調查sqllite3,你不需要在這種情況下使用異步庫作爲一個額外的(順便說一句在異步functins你必須調用與返回參數回調就可以了)。 In sqllite3 documentation db.each

該最新的代碼應該可以工作。嘗試下面。

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

是的我知道,但這不是問題,我更新我的代碼到8080和同樣的問題。 – Silvering

+0

@Sververing我更新了答案。 –

+0

感謝您的幫助。不過同樣的問題:https://gist.github.com/anonymous/6fe238a282320f9707ea – Silvering