Node.js 个回调。错误 [TypeError]:回调不是函数
Node.js callbacks. Error [TypeError]: callback is not a function
我是 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
这不是一个函数。
我是 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
这不是一个函数。