我正在编写一个 api 来通过 id 获取员工,但我遇到的问题是控制器调用服务中的另一个函数并将参数和回调传递给该函数。但是当函数调用回调时,我得到一个错误。代码如下。我查看了在线示例并尝试对其进行故障排除,但失败了。我对没有参数的 getall 方法有类似的设置,它似乎工作正常。代码如下。
控制器代码是。
function getById(req, res, next) {
userService.getById(req.params.id, (results) => {
res.json(results);
})
.catch(err => next(err));
}
服务代码是。
async function getById(id, callback) {database.query('SELECT * FROM employee WHERE id =' +id, (err, results) => {
if (err) {
callback(err);
} else
callback(results);
});
}
我得到的错误是:
TypeError: callback is not a function
at Query.database.query (/user.service.js:52:7)
at Query.<anonymous> (/Connection.js:525:10)
at Query._callback (/Connection.js:491:16)
at Query.Sequence.end (/Sequence.js:83:24)
at Query.ErrorPacket (/Query.js:90:8)
at Protocol._parsePacket (/Protocol.js:291:23)
at Parser._parsePacket (/Parser.js:433:10)
at Parser.write (/Parser.js:43:10)
at Protocol.write (/Protocol.js:38:16)
at Socket.<anonymous> (/Connection.js:91:28)
at Socket.<anonymous> (/Connection.js:525:10)
at Socket.emit (events.js:182:13)
at addChunk (_stream_readable.js:283:12)
at readableAddChunk (_stream_readable.js:264:11)
at Socket.Readable.push (_stream_readable.js:219:10)
at TCP.onread (net.js:639:20)