I have defined two simple functions in nodejs:
我在node . js中定义了两个简单的函数:
function findEntityDetailsAsIs(modelname, callback) {
modelname.find({}, function(error, result) {
if (error)
callback (error, null);
else {
//console.log(result);
callback (null, result);
}
});
};
This is my first function and another function is
这是第一个函数,另一个函数是
function printEntitydetails(error, entitydetails, callback) {
console.log(entitydetails);
}
I am trying to call these functions as such
我试着把这些函数称为。
findEntityDetailsAsIs(fieldLabel, printEntitydetails(error, entitydetails));
But as i try to run this function call it throws an error
但是当我尝试运行这个函数时它会抛出一个错误
ReferenceError: error is not defined
But error is just as placeholder object i am passing from callback.
但是error只是我从回调中传递的占位符对象。
findEntityDetailsAsIs(fieldLabel, printEntitydetails(entitydetails));
I tried skipping error in the call but this time it gives this error.
我尝试在调用中跳过错误,但这次它给出了这个错误。
ReferenceError: entitydetails not is not defined
As per my knowledge the findEntityDetailsAsIs
should provide the context for the entitydetails
, as i have provided a callback(null, result)
.
据我所知,findEntityDetailsAsIs应该为entitydetails提供上下文,因为我提供了回调(null, result)。
1 个解决方案
#1
3
Your function findEntityDetailsAsIs
expects to get the callback function, not the execution result of it.
You need to provide only the function name:
函数findEntityDetailsAsIs希望获得回调函数,而不是它的执行结果。您只需要提供函数名:
findEntityDetailsAsIs(fieldLabel, printEntitydetails);
When you run it like you do, you pass to findEntityDetailsAsIs
the result of printEntitydetails
instead of the function itself. since the function returns nothing, you get undefined
当您像这样运行它时,您将把printEntitydetails而不是函数本身的结果传递给findEntityDetailsAsIs。由于函数没有返回任何内容,所以您将没有定义。
#1
3
Your function findEntityDetailsAsIs
expects to get the callback function, not the execution result of it.
You need to provide only the function name:
函数findEntityDetailsAsIs希望获得回调函数,而不是它的执行结果。您只需要提供函数名:
findEntityDetailsAsIs(fieldLabel, printEntitydetails);
When you run it like you do, you pass to findEntityDetailsAsIs
the result of printEntitydetails
instead of the function itself. since the function returns nothing, you get undefined
当您像这样运行它时,您将把printEntitydetails而不是函数本身的结果传递给findEntityDetailsAsIs。由于函数没有返回任何内容,所以您将没有定义。