错误:在 npm request-context

问题描述

我正在使用 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 方法中跟踪此问题时发现的问题序列

  1. 这一行 app.use(contextService.middleware('provider')); 将函数 runInContextMiddleware(req,next) 返回给 app.use(),但它永远不会在 app.use 内发出。
  2. app.use中,
if (!fn || !fn.handle || !fn.set) {
      return router.use(path,fn);
    }

其中,fn 是 runInContextMiddleware(req,next) 并且 handleset 属性未定义 fn 因此它不执行 fn.emit('mount',this);

  1. 因此,永远不会设置上下文,而这一行 console.log(contextService.getContext('provider')); 给出了 undefined
  2. 最后,setContext() 调用 getCurrent(current) 并将 domain.active 存储在 current 中,即 null

这就是我面对错误的方式。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...