如何使用回调调用递归函数?

时间:2017-03-29 13:32:13

标签: node.js recursion

当用callback函数递归调用函数时,它会抛出一个错误,即

  

错误:已经调用了回调。       at /home/clsah/projects/LMS/node_modules/async/dist/async.js:837:32      ....

这是我的代码: -

function getEventList(oauth2Client, user, callback) {
    var calendar = google.calendar('v3');
    calendar.events.list({
        auth: oauth2Client,
        calendarId: 'primary',
        timeMin: (new Date()).toISOString(),
        maxResults: 50,
        singleEvents: true,
        orderBy: 'startTime'
    }, function(err, response) {
        if (err) {
            generateNewToken(oauth2Client, function(err, oauth2Client) {
                if (err)
                    return callback(err)
                getEventList(oauth2Client, user, callback); //here is problem
            })
        } else {
            var events = response.items || [];
            if (events.length == 0) {
                console.log('No upcoming events found.');
            } else {
                for (var i = 0; i < events.length; i++) {
                    events[i].userId = user._id;
                }
            }
            console.log('getEventList', events)
            callback(null, events);
        }

    });
}

0 个答案:

没有答案