首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Node.js process.domain有时是未定义的

Node.js process.domain有时是未定义的
EN

Stack Overflow用户
提问于 2015-02-27 12:10:46
回答 3查看 3.3K关注 0票数 6

在使用Node.js构建LoopbackJS的项目中,我需要在请求期间存储数据。

所以我使用了域名特性:

代码语言:javascript
复制
// pre-processing middleware
app.use(function (req, res, next) {
  // create per request domain instance
  var domain = require('domain').create();

  // save request and response to domain, to make it accessible everywhere
  domain.req = req;
  domain.res = res;
  domain.run(next);
});

稍后在所需模块中:

代码语言:javascript
复制
Model.beforeRemote('**', function(oContext, oModel, next) {
    // Save method name for later use
    process.domain.remoteContext = {          /* Here is an error thrown */
      methodName: oContext.method.name
    };
    ...
    process.domain.res.send() // example of usage
})

但是当我从Safari或IE发出请求时,process.domain有时是没有定义的!从Chrome或Firefox请求按预期工作。有什么建议吗?

错误响应:

代码语言:javascript
复制
{"error":{"name":"TypeError","status":500,"message":"Cannot set property 'remoteContext' of undefined","stack":"TypeError: Cannot set property 'remoteContext' of undefined\n    at module.exports (/Users/igormatyushkin/projects/Yash/server/hooks/admin-remote.js:12:34)\n    at Function.Model.setup.ModelCtor.beforeRemote.args (/Users/igormatyushkin/projects/Yash/node_modules/loopback/lib/model.js:184:9)\n    at execStack (/Users/igormatyushkin/projects/Yash/node_modules/loopback/node_modules/strong-remoting/lib/remote-objects.js:363:13)\n    at RemoteObjects.execHooks (/Users/igormatyushkin/projects/Yash/node_modules/loopback/node_modules/strong-remoting/lib/remote-objects.js:372:10)\n    at RemoteObjects.invokeMethodInContext (/Users/igormatyushkin/projects/Yash/node_modules/loopback/node_modules/strong-remoting/lib/remote-objects.js:512:8)\n    at async.series.results (/Users/igormatyushkin/projects/Yash/node_modules/loopback/node_modules/strong-remoting/node_modules/async/lib/async.js:610:21)\n    at _asyncMap (/Users/igormatyushkin/projects/Yash/node_modules/loopback/node_modules/strong-remoting/node_modules/async/lib/async.js:249:17)\n    at async.eachSeries.iterate (/Users/igormatyushkin/projects/Yash/node_modules/loopback/node_modules/strong-remoting/node_modules/async/lib/async.js:149:13)\n    at async.eachSeries (/Users/igormatyushkin/projects/Yash/node_modules/loopback/node_modules/strong-remoting/node_modules/async/lib/async.js:165:9)\n    at _asyncMap (/Users/igormatyushkin/projects/Yash/node_modules/loopback/node_modules/strong-remoting/node_modules/async/lib/async.js:248:13)"}}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-03-12 15:59:38

刚刚发现,在创建的时候,说明了将req和res绑定到域,解决了执行堆栈中的未定义域。

代码语言:javascript
复制
app.use(function (req, res, next) {
  // create per request domain instance
  domain.create().run(function() {
    // explicit binding
    process.domain.add(req)
    process.domain.add(res)
    // save request to domain, to make it accessible everywhere
    process.domain.req = req;
    process.domain.res = res;
    next()
  });
});

不知道为什么会这样,所以我把问题留给其他可能的建议。

票数 2
EN

Stack Overflow用户

发布于 2015-03-12 12:48:02

值得一提的是,域不用于以这种方式处理错误。。相反,域被用来允许整个应用程序的模块安全崩溃(例如,您可能希望在沙箱中运行第三方代码)。在这里没有必要使用域(当然,您可以免费使用)

此外,只有在给定函数从域堆栈中执行时,才会设置process.domain。您没有提供有关model调用站点的信息,因此我只能假设您的模型是从此堆栈外部调用的,这将导致错误。

考虑到您正在链接到beforeRemote,它在执行远程操作之前执行,我将假设这是从域堆栈外部调用的,因此未定义的process.domain是预期的行为。

票数 1
EN

Stack Overflow用户

发布于 2016-08-10 20:01:56

对于这个问题的读者来说,重要的是要知道节点域模块已经被废弃,不应该被使用。

如果您到这里来寻找解决这个问题的方法,那么您最好找的是域模块以外的其他东西。

此模块正在挂起弃用。一旦替换API最后确定,这个模块将被完全废弃。大多数最终用户不应该有理由使用这个模块。绝对必须具备域所提供的功能的用户可能暂时依赖它,但将来应该不得不迁移到不同的解决方案。

虽然这在技术上没有回答OP的问题,但我相信对于大多数读者来说,这是正确的“解决方案”。

使用域的一个替代方法是延续-本地存储模块。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28764830

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档