Node.js回调.错误[TypeError]:回调不是函数



我是Node.js、Javascript和回调的新手。我正在尝试编写一个非常简单的程序,但我无法让回调正常工作。

这是相关代码:

var keysfetched = false;
var urlsfetched = false;

function setKeysfetched(){
keysfetched = true;
}

function setUrlsfetched(){
urlsfetched = true;
} 

//get list of all media in bucket
getKeys(setKeysfetched);
//get a list of all media urls in DB
getUrls(setUrlsfetched);
//check for media in the bucket which is not used
checkKeys();


function getKeys(callback) {

S3.listObjectsV2(params, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else{
var contents = data.Contents;
contents.forEach(function (content) {
allKeys.push(content.Key);
});

if (data.IsTruncated) {
params.ContinuationToken = data.NextContinuationToken;
//console.log("get further list...");
getKeys();
}
else{
console.log("end of loop...");
}
}
});
callback()
}

当我运行这个时,我得到一个错误:错误[TypeError]:回调不是函数

如果我注释掉getKeys((中的所有代码,我就不会得到错误。

这运行得很好:

function getKeys(callback) {
//Hard work here
callback()
}

我做错了什么?

您正在传递callback并在getKeys的底部调用它,但在内部您没有传递任何回调

if (data.IsTruncated) {
params.ContinuationToken = data.NextContinuationToken;
//console.log("get further list...");
getKeys();
}

所以它试图调用不是函数的undefined

最新更新