W kompilacji projektu Node.js z LoopbackJS muszę przechowywać dane podczas żądania.Node.js proces.domain jest niezdefiniowany czasami
Więc użyłem domain cechę:
// 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);
});
Później w wymaganym modułem:
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
})
ale kiedy zrobić wniosek Safari lub IE, process.domain czasami niezdefiniowana! Żądanie z przeglądarki Chrome lub Firefox działa zgodnie z oczekiwaniami. Jakieś sugestie?
odpowiedź Błąd:
{"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)"}}
Czy możesz pokazać nam kod, w którym domena jest niezdefiniowana? Czy uzyskujesz dostęp do domeny z tego samego zakresu, w którym ją zdefiniowałeś? W twoim przykładzie kodu faktycznie nie przypisujesz domeny do procesu - zakładam, że robisz to gdzie indziej w twoim kodzie?Proszę również opublikować ślad stosu prowadzący do niezdefiniowanego błędu, może on zawierać wskazówkę lub dwie. –
@RobertRossmann, zaktualizowałem kod i dodano zgłoszony błąd. "Process.domain" jest inicjowane przez węzeł, gdy wywoływane jest 'domain.run()' i mam dostęp do 'process.domain.remoteContext' później w innych wymaganych modułach. – IvanZh