node.js async:未调用最后一个回调函数

时间:2018-03-31 19:24:23

标签: javascript node.js express async.js

我正在关注MDN tutorials for web development并遵循那里给出的所有步骤。一切正常,直到part 4,但在按照this更改我的代码后的第5部分中,网站没有响应。

我只是复制粘贴所有代码,所以我不应该犯任何错误。

在代码中放入console.log()语句之后,我发现最后一个回调函数没有被调用。

教程中给出的原始代码是:

var Book = require('../models/book');
var Author = require('../models/author');
var Genre = require('../models/genre');
var BookInstance = require('../models/bookinstance');

var async = require('async');

exports.index = function(req, res) {   

    async.parallel({
        book_count: function(callback) {
            Book.count({}, callback); // Pass an empty object as match condition to find all documents of this collection
        },
        book_instance_count: function(callback) {
            BookInstance.count({}, callback);
        },
        book_instance_available_count: function(callback) {
            BookInstance.count({status:'Available'}, callback);
        },
        author_count: function(callback) {
            Author.count({}, callback);
        },
        genre_count: function(callback) {
            Genre.count({}, callback);
        },
    }, function(err, results) {
        res.render('index', { title: 'Local Library Home', error: err, data: results });
    });
};

对于调试我改为:

exports.index = function(req, res) {
    console.log("Book controller index called");   

    async.parallel({
        book_count: function(callback) {
            console.log("Book controller book_count called");
            //Book.count({}, callback); // Pass an empty object as match condition to find all documents of this collection
            Book.count({}, function(err, count){
                callback(err, count);
            });
            console.log("Book controller book_count ended");
        },
        book_instance_count: function(callback) {
            console.log("Book controller book_instance_count called");
            //BookInstance.count({}, callback);
            BookInstance.count({}, function(err, count){
                callback(err, count);
            });
            console.log("Book controller book_instance_count ended");
        },
        book_instance_available_count: function(callback) {
            console.log("Available books count called");
            //BookInstance.count({status:'Available'}, callback);
            BookInstance.count({status:'Available'}, function(err, count){
                callback(err, count);
            });
            console.log("Available books count ended.");
        },
        author_count: function(callback) {
            console.log("Author count called.");
            //Author.count({}, callback);
            Author.count({}, function(err, count){
                callback(err, count);
            });
            console.log("Author count ended.");
        },
        genre_count: function(callback) {
            console.log("Genre count called.");
            //Genre.count({}, callback);
            Genre.count({}, function(err, count){
                callback(err, count);
            });
            console.log("Genre count ended.");
        },
    }, function(err, results) {
        console.log("Book controller render called");
        res.render('index', { title: 'Local Library Home', error: err, data: results });
    });
};

但它并没有改变任何东西,即网站仍未响应。

console.log()日志是:

  

书籍控制器索引称为   书籍控制器book_count称为
  书籍管理员book_count结束了   书籍控制器book_instance_count称为
  书籍控制器book_instance_count结束了   可用的书籍数量称为
  可用的图书计数结束。
  作者计数称。
  作者数量已结束。
  流派计数称。
  流派数量已经结束。

第4部分教程的项目是here

根据Amir的建议,我将log()放在回调函数中:

book_count: function(callback) {
            console.log("Book controller book_count called");
            //Book.count({}, callback); // Pass an empty object as match condition to find all documents of this collection
            Book.count({}, function(err, count){
                console.log("before book count callback");
                callback(err, count);
                console.log("after book count callback");
            });
            console.log("Book controller book_count ended");
        }

控制台日志仍然相同,即console.log("before book count callback");console.log("after book count callback");未被调用。

0 个答案:

没有答案