我正在写一个返回结果或错误的方法。
myMethod.methodName = (param1, param2, param3, error, callback) => {
try {
myDB.findOne({ param1: value}, function (err, result) {
if (err) {
throw(err);
}
if (result) {
callback(result);
});
} catch (e) {
error(e);
}
};
从调用函数,我按如下方式调用它:
myExport.data.methodName(param1, param2, param3, (err, result) => {
if (err) {
console.log("Result error:", err);
}
if (result) {
console.log('result : ', result);
}
});
在这里我遇到了一个错误:'TypeError:回调不是函数
如何实现返回错误或结果的方法调用以及如何处理?
最佳答案
methodName()
需要5个参数,第5个是回调。调用方法时,您将回调作为第四个参数传递,因此methodName()
内部的回调实际上为空。因此,错误。
您的 call 者是正确的。像这样更改方法定义:
myMethod.methodName = (param1, param2, param3, callback) => {
try {
myDB.findOne({ param1: value}, function (err, result) {
if (err) {
return callback(err);
}
return callback(null, result);
//rest of the code as-is
...
关于node.js - Node.js结果和错误处理-'TypeError : callback is not a function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50301102/