问题描述
我正在使用 npm 请求上下文。 我收到此错误:
未找到设置属性提供程序的活动上下文
当我尝试使用 set()
函数设置上下文时。
代码如下:
const contextService = require("request-context");
router.use('/*',(req,res,next) => {
Provider.findOne({ _id: req.providerId },(err,provider) => {
if (provider) {
app.use(contextService.middleware('provider'));
console.log(contextService.getContext('provider')); // undefined
contextService.set('provider',res.locals.provider); // ERROR - No active context found to set property provider
}
next();
});
}
我在 request-context/lib/index.js
文件和 app.use()
文件中的 express/lib/application.js
方法中跟踪此问题时发现的问题序列
- 这一行
app.use(contextService.middleware('provider'));
将函数runInContextMiddleware(req,next)
返回给app.use()
,但它永远不会在app.use
内发出。 - 在
app.use
中,
if (!fn || !fn.handle || !fn.set) {
return router.use(path,fn);
}
其中,fn 是 runInContextMiddleware(req,next)
并且 handle
和 set
属性未定义 fn
因此它不执行 fn.emit('mount',this);
- 因此,永远不会设置上下文,而这一行
console.log(contextService.getContext('provider'));
给出了undefined
。 - 最后,
setContext()
调用getCurrent(current)
并将domain.active
存储在current
中,即null
。
这就是我面对错误的方式。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)