0

我正在尝试串联调用 3 个函数,但是最后一个函数也有一个 for 循环,我有下面的代码,但是当我运行它时,我得到了callback()调用之后的错误calanderItems.push()

if (called) throw new Error("Callback was already called.");


 async.series([

    function(callback){
        //some database query
                    callback();
    },
    function(callback){
                    //other database query
        callback();
    },
    function(callback){
        var google_calendar = new gcal.GoogleCalendar(req.user.accessToken);
        var calenderItems = [];
        google_calendar.calendarList.list(function(err, calendarList) {
            async.each(calendarList.items, function (item, callback){ 
                google_calendar.events.list(item.id, function(err, calenderItem) {
                    calenderItems.push(calenderItem);
                    callback();
                });
            },function(err){
                for (var element in prettyArray){
                    calenderitems.push(prettyArray[element])
                }
                console.log(calenderitems);
            });
        });
    }

]);
4

1 回答 1

0

我认为您应该按如下方式更改代码:

if (called) throw new Error("Callback was already called.");


 async.series([

    function(callback){
        //some database query
                    callback();
    },
    function(callback){
                    //other database query
        callback();
    },
    function(callback){
        var google_calendar = new gcal.GoogleCalendar(req.user.accessToken);
        var calenderItems = [];
        google_calendar.calendarList.list(function(err, calendarList) {
            async.each(calendarList.items, function (item, callback){ 
                google_calendar.events.list(item.id, function(err, calenderItem) {
                    calenderItems.push(calenderItem);
                    callback();
                });
            },function(err){
                for (var element in prettyArray){
                    calenderItems.push(prettyArray[element]) // Be careful, you were using a variable that was not in the scope (calenderitems)
                }
                console.log(calenderItems);
                callback(); // This is necessary
            });
        });
    }

]);
于 2021-06-13T19:30:59.267 回答