簡體   English   中英

Node.js / Express異步功能

[英]Node.js/Express Async functions

我正在玩node.js並表達。 我有一個小服務器,可獲取sqlite內容並將所有內容發送到Jade模板。 使用此代碼可以正常工作:

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

但是網頁正在加載,加載,並且什么也沒有發生。.我做錯了什么,但暫時不知道在哪里。 如果有人有想法,那就太好了;-)謝謝!

您的網址錯誤,第二個代碼阻止您的端口也不同。

在域名或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,在這種情況下您不需要使用異步庫(異步函數中的BTW您必須調用帶有返回參數的回調)。 在sqllite3文檔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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM