-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How can I pass data between hooks #766
Comments
Add them to the Customer.beforeRemote('**', function(ctx, unused, next) {
ctx.modelData = "A";
next();
});
Customer.afterRemote('**', function(ctx, unused, next) {
console.log('modelData', ctx.modelData);
}); |
See #337 for a more advanced solution that can be used to access context data from any method. |
👍 cool, thanks for your help |
@bajtos module.exports = function(modelName) {
// I'd like to get the context here, is there any way ?
var a = modelName.[somehow].ctx.modelData;
// or even in model hook ?
modelName.beforeSave = function(next, mdl) {
var a = mdl.[somehow].ctx.modelData;
}
} ^^ |
var ctx = loopback.getCurrentContext(); |
There is no context at the time your model script is run, as the context is created always per incoming request.
This is where you can call Keep in mind that module.exports = function(Customer) {
Customer.beforeRemote('**', function(remotingCtx, unused, next) {
loopbackCtx = loopback.getCurrentContext();
// save the value from `?someQueryParam=value`
loopbackCtx.modelData = remotingCtx.req.query.someQueryParam;
next();
});
Customer.beforeSave = function(next, mdl) {
loopbackCtx = loopback.getCurrentContext();
this.contextData = loopbackCtx.modelData; // the value stored above
next();
};
}; |
@bajtos thanks for your answer ^^ but I got error when trying to get "loopbackCtx" with "loopback.getCurrentContext()" module.exports = function(Customer) {
Customer.beforeRemote('**', function(remotingCtx, unused, next) {
loopbackCtx = loopback.getCurrentContext();
// save the value from `?someQueryParam=value`
loopbackCtx.modelData = remotingCtx.req.query.someQueryParam;
next();
}); returned error is about "has no method 'getCurrentContext" "name": "TypeError",
"status": 500,
"message": "Object function createApplication() {\n var app = express();\n\n merge(app, proto);\n\n app.loopback = loopback;\n\n // Create a new instance of models registry per each app instance\n app.models = function() {\n return proto.models.apply(this, arguments);\n };\n\n // Create a new instance of datasources registry per each app instance\n app.datasources = app.dataSources = {};\n\n // Create a new instance of connector registry per each app instance\n app.connectors = {};\n\n // Register built-in connectors. It's important to keep this code\n // hand-written, so that all require() calls are static\n // and thus browserify can process them (include connectors in the bundle)\n app.connector('memory', loopback.Memory);\n app.connector('remote', loopback.Remote);\n\n return app;\n} has no method 'getCurrentContext'",
"details": null I did something wrong or anything missing ? :( |
See the discussion in #775. You must ensure that |
Actually, the context middleware was not released yet. You have to use loopback from github master to get this new feature:
|
"Keep in mind that loopback.getCurrentContext() is different from the ctx argument passed to
beforeRemote and afterRemote hooks.". @bajtos And another problem is when i request with accessToken was set: "loobpack.getCurrentContext" return null. |
They are different objects created at different places, thus they don't share the same properties. Also you have to use
That happens when
|
@bajtos
My code: var loopback = require('loopback');
module.exports = function(Customer) {
Customer.beforeRemote('**', function(remotingCtx, unused, next) {
loopbackCtx = loopback.getCurrentContext();
console.log("beforeRemote", loopbackCtx);
loopbackCtx.modelData = 'aaaaaaaaaaaaaaa';
next();
});
Customer.beforeSave = function(next, customer) {
loopbackCtx = loopback.getCurrentContext();
console.log("beforeSave", loopbackCtx);
next();
};
}; and my log:
|
Where can i place both of them ? |
Please open a new issue to track this problem and repost the steps, the code and the log there. |
ok, thanks |
@bajtos thanks for your answers, I am currently working with @bluestaralone and we are looking forward to your support on #809 :) |
Hi,
I would like to pass data between hooks, but don't know how, if I set it as global variable under module.export in modelName.js, it's not safe between requests.
for example:
thanks for any help :)
@bajtos do you have any idea ^^
The text was updated successfully, but these errors were encountered: