我在nodejs中定义了两个简单的函数:
function findEntityDetailsAsIs(modelname, callback) {
modelname.find({}, function(error, result) {
if (error)
callback (error, null);
else {
//console.log(result);
callback (null, result);
}
});
};
这是我的第一个函数,另一个函数是
function printEntitydetails(error, entitydetails, callback) {
console.log(entitydetails);
}
我试图这样调用这些函数
findEntityDetailsAsIs(fieldLabel, printEntitydetails(error, entitydetails));
但是当我尝试运行这个函数调用时它会抛出一个错误
ReferenceError: error is not defined
但是错误就像我从回调传递的占位符对象一样。
findEntityDetailsAsIs(fieldLabel, printEntitydetails(entitydetails));
我尝试跳过调用中的错误,但这次它给出了此错误。
ReferenceError: entitydetails not is not defined
据我所知,findEntityDetailsAsIs
应该为entitydetails
提供上下文,因为我已经提供了callback(null, result)
。
最佳答案
您的函数findEntityDetailsAsIs
期望获取回调函数,而不是其执行结果。
您只需提供函数名称:
findEntityDetailsAsIs(fieldLabel, printEntitydetails);
当您像这样运行它时,您将 printEntitydetails
的结果而不是函数本身传递给 findEntityDetailsAsIs
。由于该函数没有返回任何内容,因此您得到未定义
关于javascript - 尝试使用函数组合时出现 ReferenceError : error is not defined,,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46233158/