【发布时间】:2021-08-06 09:48:32
【问题描述】:
我在 NodeJS 中使用 express 中间件。我组织了我的路由没有回调,但是对于其中一些最好是互惠函数,所以我开始写一些回调。
我有例如这个函数(我没有放整个代码,在这里对我的问题没用):
async function verification(req, res, next) {
我在路由部分这样使用它:
router.post('/item/:id/dosomething',
verification,
async (req, res) => {
一切正常,但如果我想继续使用回调(有时它是一个干净高效的代码的好解决方案),我必须将一些参数传递给函数。当然我试过了,但它没有用:
router.post('/item/:id/dosomething',
verification(arg1, arg2),
async (req, res) => {
我在 StackOverflow 上搜索了“将参数传递给回调函数”的答案,但少数有趣的人谈到了包装函数,我认为我无法实现。
如果有人能帮我一把,那就太好了。谢谢:)
这是一个关于 next 如何在没有在调用中写入它的情况下工作的 sn-p(查看 verif):
回调:
async function verif(req, res, next) {
let rows;
const {
id
} = req.params;
({ rows } = await db.query(`XXX`))
if (rows.length === 1) {
return next();
} else {
retour(req, res, 500, "No can do.");
}
名称:
router.post('/mymusic/:id/addElements',
droits.verifRightsCB.bind(undefined, 'music', 'addInMyMusic'),
verif,
async (req, res) => {...
路由的最后一部分(async (req, res) =>)只有在next()条件在verif中满足时才会执行,虽然我没有传递任何参数。
【问题讨论】: