无法导出功能
Posted
技术标签:
??????????????????:Can not export function ??????:2019-10-27 19:11:52 ??????:???????:
exports.get = async function (req, res, next)
filter.validateId(req,res,next);
const db = req.app.get('db');
let id = req.params.id;
let user;
try
user = await db.models.user.findByPk(id);
catch (err)
res.send(400)
res.json(user);
;
?????:
var validateId = function (req, res, next)
let id = parseInt(req.params.id);
if (isNaN(id) || id <= 0)
res.send(400);
res.locals.id = id;
return next();
;
exports.validateId=validateId;
???????:
GET /users/203 404 17.862 ?? - 90
(node:20775) UnhandledPromiseRejectionWarning: ?? [ERR_HTTP_HEADERS_SENT]: ????????????? ? ServerResponse.setHeader (_http_outgoing.js:470:11) ? ServerResponse.header (/home/qt/dev/BigDealExpressJSTest/node_modules/express/lib/response.js:767:10) ? ServerResponse.send (/home/qt/dev/BigDealExpressJSTest/node_modules/express/lib/response.js:170:12) ? ServerResponse.json (/home/qt/dev/BigDealExpressJSTest/node_modules/express/lib/response.js:267:15) ?exports.get (/home/qt/dev/BigDealExpressJSTest/controllers/userController.js:28:7) (??:20775)UnhandledPromiseRejectionWarning:????????????????? catch ?????????????,?????? .catch() ?????? (????:1) (??:20775)[DEP0018] DeprecationWarning:????????????????,???? Promise ????????????? Node.js ???
??????:
??Cannot set headers after they are sent
???????????,?????validateId
?,???res.json(user)
??,??? if,???????? UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1) (node:21582) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
?????1?:
??????????:
??:
if (isNaN(id) || id <= 0)
res.send(400); //return res.send(400);
????return
,??????????next()
???????
????????:
?validateId
?,?????get
???next
???,???? validateId
?? next ?,???????? next
? get
,??????????????????
??,???? get
????? filter.validateId(req,res,next);
??????????????
??
exports.get = async function (req, res, next)
const db = req.app.get('db');
let id = req.params.id;
let user;
try
user = await db.models.user.findByPk(id);
catch (err)
res.send(400)
res.json(user);
;
app.get('/route', validateId, get , (req,res)=>.. do something )
?app.get('/route', validateId, get)
????:
以上是关于无法导出功能的主要内容,如果未能解决你的问题,请参考以下文章
无法解决“ImportError:动态模块未定义模块导出功能”