首页 > 解决方案 > res.send() sends Type Error: callback is not a function - although whole procedure runs correct (async.waterfall)

问题描述

I'm writing a function for my Node.js server (using express as a framework, MongoDB as the database). I have to call two mongoose queries:

(1) Find data filtered by parameters of one collection

(2) Aggregate data of another collection (needs result from the first query because one attribute gets appended to these results)

I now use the async.waterfall a function that makes it possible to ensure that the second operation only gets triggered after the first one has finished.

The variable resultResponse (at the end of my code snippet) has the right value when I run the function.

The problem I am struggling with:

The server throws the error TypeError: callback is not a function for the first callback. This type error is also the answer I receive via postman calling /getRoutes. So res.send() doesn't send the variable resultResponse back (which is correct), but the error.

Does anyone know, how I can solve this problem? Thanks a lot!

    let async = require('async');

    app.get('/getRoutes', function (req, res) {

    var param = req.query;
    let resultResponse;

    async.waterfall([

(1)

        function (param, callback) {
            let dataCallback;
            // first mongoose operation
            Route.find(param).lean().exec(function (err, data) {
                if (err)
                    throw err;
                dataCallback = data;
            }
                .then(callback(null, dataCallback)));
        }, 

TypeError: callback is not a function

(2)

        function (aRoutes, callback) {
            let oRoutes = [];
            for (let i in aRoutes) {
            // second mongoose operation
                Rating.aggregate([{$match: {route: aRoutes[i]._id}}
                    , {
                        $group:
                            {_id: null, rating: {$avg: '$rating'}}
                    }
                ]).then(function (response) {
                    let oneRoute = aRoutes[i];
                    let avgRating = response[0].rating;
                    oneRoute.avg_rating = avgRating;

                    oRoutes.push(oneRoute);

                    if (i == (aRoutes.length - 1)) {
                        callback(null, oRoutes);
                    }
                });
            }
        }],
    function (err, result) {
        if (err) throw err;
        resultResponse = result; //console.log -> right result here
    });

res.send(resultResponse); });

标签: javascriptnode.jsmongodbexpresscallback

解决方案


您的第一个函数将寻求callback函数作为第一个参数。在这里查看更多详情。param是全局定义的,所以你不需要通过。

function (callback) {
  let dataCallback;
  // first mongoose operation
  Route.find(param).lean().exec(function (err, data) {
    if (err)
      throw err;
    dataCallback = data;
 }).then(function(){
   callback(null, dataCallback);
 });
}

或者

只需返回data而不是创建undefined对象。

function (callback) {
  // first mongoose operation
  Route.find(param).lean().exec(function (err, data) {
    if (err)
      throw err;
    return data;
 }).then(function(dataCallback){
   callback(null, dataCallback);
 });
}

推荐阅读