在 promise 链中调用 express 中间件 next()
Call express middlewares next() inside a promise chain
我正在尝试构建一个加载请求的参数中间件 object
并将其附加到 request object
,这样我就不必一遍又一遍地编写相同的代码。
我正在使用 Sequelize
作为 ORM 来访问基于 Promise (bluebird
) 的 MySQL 数据库。
代码如下:
router.param('id', function (req, res, next, id) {
return models.Object.findById(id)
.then((object) => {
//Check if the requested object was found
if (object) {
//Append the request object to the request object
//for further usage in upcoming route handlers
req.object = object
return next()
} else {
//Throw an error, so that it can be caught
//by the catch block of the promise chain
let err = new Error('Object not found.')
err.status = 404
throw err
}
})
//Catch any error and forward it to the
//next express error handler
.catch((err) => { return next(err) })
})
如你所见,我正在检查请求的对象是否存在,如果存在,我将其追加到 express 的请求对象中。如果不是,我会抛出一个错误。
当我 运行 此代码时,我收到警告:
Warning: a promise was created in a handler at /*path goes here*/ but was not returned from it
。
在这一点上,我真的不知道如何摆脱那个错误。
编辑:
我尝试了Hosar的解决方案,但不幸的是仍然出现警告。这是我使用的确切代码:
router.param('aId', function (req, res, next, aId) {
models.Authorization.findById(aId)
.then((authorization) => {
if (authorization) {
req.authorization = authorization
next()
} else {
let err = new Error('Not Found')
err.status = 404
next(err)
}
})
.catch((err) => { return next(err) })
})
错误发生在我想调用不带任何参数的next() 的那一行。
完整的警告是:
(node:5504) Warning: a promise was created in a handler at C:\Users\dkaiser
\repos\lead\lead_backend\routes\auth.js:10:16 but was not returned from it
问题是你在return许诺。只需避免使用第一个 return。
尝试:
router.param('id', function (req, res, next, id) {
models.Object.findById(id)
.then((object) => {
//Check if the requested object was found
if (object) {
//Append the request object to the request object
//for further usage in upcoming route handlers
req.object = object
next()
} else {
//Throw an error, so that it can be caught
//by the catch block of the promise chain
let err = new Error('Object not found.')
err.status = 404
next(err);
}
})
//Catch any error and forward it to the
//next express error handler
.catch((err) => { next(err) })
})
您可以使用 Node v7.6 中的异步中间件功能直接从已解析的 Promise 中提取数据
router.param('id', async function (req, res, next, id) {
const object = await models.Object.findById(id);
if (object) {
req.object = object;
next();
} else {
let err = new Error('Object not found.')
err.status = 404
next(err);
}
});
我正在尝试构建一个加载请求的参数中间件 object
并将其附加到 request object
,这样我就不必一遍又一遍地编写相同的代码。
我正在使用 Sequelize
作为 ORM 来访问基于 Promise (bluebird
) 的 MySQL 数据库。
代码如下:
router.param('id', function (req, res, next, id) {
return models.Object.findById(id)
.then((object) => {
//Check if the requested object was found
if (object) {
//Append the request object to the request object
//for further usage in upcoming route handlers
req.object = object
return next()
} else {
//Throw an error, so that it can be caught
//by the catch block of the promise chain
let err = new Error('Object not found.')
err.status = 404
throw err
}
})
//Catch any error and forward it to the
//next express error handler
.catch((err) => { return next(err) })
})
如你所见,我正在检查请求的对象是否存在,如果存在,我将其追加到 express 的请求对象中。如果不是,我会抛出一个错误。
当我 运行 此代码时,我收到警告:
Warning: a promise was created in a handler at /*path goes here*/ but was not returned from it
。
在这一点上,我真的不知道如何摆脱那个错误。
编辑:
我尝试了Hosar的解决方案,但不幸的是仍然出现警告。这是我使用的确切代码:
router.param('aId', function (req, res, next, aId) {
models.Authorization.findById(aId)
.then((authorization) => {
if (authorization) {
req.authorization = authorization
next()
} else {
let err = new Error('Not Found')
err.status = 404
next(err)
}
})
.catch((err) => { return next(err) })
})
错误发生在我想调用不带任何参数的next() 的那一行。
完整的警告是:
(node:5504) Warning: a promise was created in a handler at C:\Users\dkaiser
\repos\lead\lead_backend\routes\auth.js:10:16 but was not returned from it
问题是你在return许诺。只需避免使用第一个 return。
尝试:
router.param('id', function (req, res, next, id) {
models.Object.findById(id)
.then((object) => {
//Check if the requested object was found
if (object) {
//Append the request object to the request object
//for further usage in upcoming route handlers
req.object = object
next()
} else {
//Throw an error, so that it can be caught
//by the catch block of the promise chain
let err = new Error('Object not found.')
err.status = 404
next(err);
}
})
//Catch any error and forward it to the
//next express error handler
.catch((err) => { next(err) })
})
您可以使用 Node v7.6 中的异步中间件功能直接从已解析的 Promise 中提取数据
router.param('id', async function (req, res, next, id) {
const object = await models.Object.findById(id);
if (object) {
req.object = object;
next();
} else {
let err = new Error('Object not found.')
err.status = 404
next(err);
}
});