javascript - 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); });
解决方案
您的第一个函数将寻求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);
});
}
推荐阅读
- webhooks - 如何从 MS 团队到 Slack 进行 Webhook?
- unit-testing - 业力测试运行者
- python - 如何向 django admin list_editable 添加反向关系字段
- excel - 修改excel单元格值
- mysql - Applying join on date 以累积方式按天选择数据的性能问题
- python - 单击后显示时如何使用python刮掉手机
- python - Python Matplotlib:如何减少非数字类型的 x 刻度线数量
- android - React-Native TypeError: undefined is not an object 。(请解释这个错误是什么意思)
- hadoop - 使用 S3 作为 nutch 存储系统
- java - 在java中合并JSON结果