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

但是网页正在加载,加载,没有任何反应.. 我做错了什么,但暂时不知道在哪里。如果有人有想法那就太好了 ;-) 谢谢!

你的 url 是错误的,第二个代码块你的端口也不同。

在域或 ip 地址后提供端口名称,如果不是,请求将转到 /product1:8080 并且您没有这样的路由器,因此请求转到错误页面也似乎您没有任何错误处理对于 404.

尝试:http://localhost:8080/product1 or http://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 ,在这种情况下你不需要额外使用异步库(顺便说一句,在异步函数中你必须使用 return 参数调用回调)。 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);