Async.waterfall:已经在node.js上调用了回调

时间:2019-05-22 14:41:12

标签: javascript node.js asynchronous waterfall

我完全知道这里有一些问题正在试图解决我在这里提出的问题。

我遍历了所有这些方法,尝试实现在这些线程上给出但未成功的答案。我尝试实现async.waterfall()时遇到的错误是已经调用了回调。

我希望从我可以使其工作的地方获得一些指导。谢谢。

getTodayDetail: function(cId,endTime,user,callback){
        console.log('Lets begin here');
        console.log('clinic id :'+cId);
        console.log('endtime for appoinment '+endTime);
        var ett = new Date();
        ett.setSeconds(0);
        ett.setHours(0);
        ett.setMinutes(0);
        var cid = cId;
        console.log(mongoose.Types.ObjectId.isValid(cid));
        //var clinicId = mongoose.Types.ObjectId(clinicId);
        //ett.setHours(00,00,1).toISOString();
        console.log("Value of et variable after setHours function "+ett);
       // var et = moment(endTime).utcOffset("+00:00").format('ddd MMM DD YYYY');
        async.waterfall([
            function f1 (callback){
                console.log('f1 from getTodayDetails '+cid);
                console.log("endTime "+endTime);
                appointment.find({ 
                    clinicId: cid, 
                    endTime: { $gte: ett, $lte: endTime }}, function(err, appointmentdetails){
                    if(err)
                    {
                        callback(err,null);
                        console.log(err);
                        return;
                    }else
                    {
                        callback(null, appointmentdetails);
                        console.log("Success "+appointmentdetails);
                    }
                })
            },
            function f2(appointmentdetails, callback) {
                console.log("from f2 content of appointmentdetails :"+appointmentdetails.data);
                for (var i = 0; i<appointmentdetails.length; i++)
                {
                    console.log("Success f2");
                    Temppay.find(({appointmentId: appointmentdetails[i]._id}),function(err, result2){
                        if(err)
                        {
                            callback(err, null);
                            console.log(err);
                            return;
                        }else{
                            callback(null, result2, appointmentdetails);
                            console.log("from f2 content of result2 "+result2.data);
                        }
                    })
                }
            },
            function f3(result2, appointmentdetails, callback){
                for(var i = 0; i<result2.length; i++)
                {
                    console.log("f3 Success");
                    patient.find(({"_id": result2[i].patientId}), function(err, result3){
                        if(err)
                        {
                            callback(err, null);
                            console.log(err);
                            return;
                        }
                        else 
                        {
                            callback(null, result3,result2,appointmentdetails);
                        }
                    })
                }

            }
        ], function(err, result3, result2,appointmentdetails){
            if(err)
            {
                console.error(err);
                return;
            }else
            {
                var appoinmentoftoday = appointmentdetails;
                var paymentdetails = result2;
                var patientdetails = result3;
                console.log("Today's appointments"+JSON.parse(JSON.stringify(appoinmentoftoday)));
                console.log("Today's appointment payment details"+JSON.parse(JSON.stringify(result2)));
                console.log("Today's appointments patient details "+JSON.parse(JSON.stringify(result3)));

            }
        })
},

这是从终端我得到的错误

from f2 content of result2 undefined

    0|app      | Error: Callback was already called.
    0|app      |     at /home/test_user/Server/node_modules/async/dist/async.js:955:32
    0|app      |     at /home/test_user/Server/app/service/appointmentService.js:1661:29
    0|app      |     at model.Query.<anonymous> (/home/test_user/Server/node_modules/mongoose/lib/model.js:3764:16)
    0|app      |     at /home/test_user/Server/node_modules/kareem/index.js:277:21
    0|app      |     at /home/test_user/Server/node_modules/kareem/index.js:131:16
    0|app      |     at _combinedTickCallback (internal/process/next_tick.js:131:7)
    0|app      |     at process._tickDomainCallback (internal/process/next_tick.js:218:9)

1 个答案:

答案 0 :(得分:0)

您正在循环中运行多次调用回调方法。

  

应该只调用一次回调。

相反,您可以将循环结果推送到数组,然后使用该数组调用回调方法。

更新:-

function f2(appointmentdetails, callback) {
    Promise.all(appointmentdetails.map((appointment) => {
        return Temppay.find({appointmentId: appointment._id});
    })).then((results) => {
        callback(null, results, appointmentdetails);
    });
}

为什么不替换上面的方法?