-
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
loopback.getCurrentContext() return null after MongoDB call returns #809
Comments
@bajtos : we are waiting for your response. Please help us ? thanks. |
I can see the accessToken: beforeRemote { name: 'loopback', active:
{ accessToken:
{ id: 'V6RJPkSe9RhvxJ8QfBpTuYXuNFeZsGZvodLzzpSQ5cY5WQTnJe1BSVYVUxIW1nKD',
ttl: 1209600,
created: Mon Nov 17 2014 21:38:32 GMT-0800 (PST),
userId: '84' } },
_set: [ null ], |
I also get But I may be doing something wrong, waiting on #761 (: |
First of all, you have to call // correct
loopbackCtx.set('modelData', 'aaaaaaaaaaaaaaa');
// wrong
loopbackCtx.modelData = 'aaaaaaaaaaaaaaa'; As for |
Here is a full example that works for me: var loopback = require('./');
var supertest = require('supertest');
var Product = loopback.createModel('Product', { name: String });
Product.beforeRemote('**', function(remotingCtx, unused, next) {
ctx = loopback.getCurrentContext();
if (ctx) ctx.set('data', 'test');
next();
});
Product.beforeSave = function(next, data) {
ctx = loopback.getCurrentContext();
if (ctx) {
console.log('context data:', ctx.get('data'));
} else {
console.log('ctx not set');
}
next();
};
var app = loopback();
app.dataSource('db', { connector: 'memory' });
app.model(Product, { dataSource: 'db' });
app.use(loopback.rest());
supertest(app).post('/products')
.set('Content-Type', 'application/json')
.send({ name: 'phone' })
.expect(200)
.end(function(){}); Output:
Are you using MongoDB to store your data? This comment says that mongodb's driver breaks the context propagation. AFAIK there isn't any issue tracking this problem, feel free to open one in loopback-connector-mongodb. It would be even better if you could submit a patch based on the solution described in the linked comment. |
I added your example code to /server/server.js as following var loopback = require('loopback');
var boot = require('loopback-boot');
var app = module.exports = loopback();
var Product = loopback.createModel('Product', { name: String });
Product.beforeRemote('**', function(remotingCtx, unused, next) {
ctx = loopback.getCurrentContext();
if (ctx) ctx.set('data', 'test');
next();
});
Product.afterInitialize = function() {
ctx = loopback.getCurrentContext();
if (ctx) {
console.log('context data - afterInitialize :', ctx.get('data'));
} else {
console.log('context data - afterInitialize : ctx not set');
}
};
Product.beforeValidate = function(next, data) {
ctx = loopback.getCurrentContext();
if (ctx) {
console.log('context data - beforeValidate :', ctx.get('data'));
} else {
console.log('context data - beforeValidate : ctx not set');
}
next();
};
Product.afterValidate = function(next, data) {
ctx = loopback.getCurrentContext();
if (ctx) {
console.log('context data - afterValidate:', ctx.get('data'));
} else {
console.log('context data - afterValidate: ctx not set');
}
next();
};
Product.beforeSave = function(next, data) {
ctx = loopback.getCurrentContext();
if (ctx) {
console.log('context data - beforeSave :', ctx.get('data'));
} else {
console.log('context data - beforeSave : ctx not set');
}
next();
};
Product.afterSave = function(next, data) {
ctx = loopback.getCurrentContext();
if (ctx) {
console.log('context data - afterSave:', ctx.get('data'));
} else {
console.log('context data - afterSave: ctx not set');
}
next();
};
Product.beforeCreate = function(next, data) {
ctx = loopback.getCurrentContext();
if (ctx) {
console.log('context data - beforeCreate:', ctx.get('data'));
} else {
console.log('context data - beforeCreate: ctx not set');
}
next();
};
Product.afterCreate = function(next, data) {
ctx = loopback.getCurrentContext();
if (ctx) {
console.log('context data - afterCreate:', ctx.get('data'));
} else {
console.log('context data - afterCreate: ctx not set');
}
next();
};
Product.beforeUpdate = function(next, data) {
ctx = loopback.getCurrentContext();
if (ctx) {
console.log('context data - beforeUpdate :', ctx.get('data'));
} else {
console.log('context data - beforeUpdate : ctx not set');
}
next();
};
Product.afterUpdate = function(next, data) {
ctx = loopback.getCurrentContext();
if (ctx) {
console.log('context data - afterUpdate:', ctx.get('data'));
} else {
console.log('context data - afterUpdate : ctx not set');
}
next();
};
Product.beforeDestroy = function(next, data) {
ctx = loopback.getCurrentContext();
if (ctx) {
console.log('context data - beforeDestroy :', ctx.get('data'));
} else {
console.log('context data - beforeDestroy : ctx not set');
}
next();
};
Product.afterDestroy = function(next, data) {
ctx = loopback.getCurrentContext();
if (ctx) {
console.log('context data - afterDestroy:', ctx.get('data'));
} else {
console.log('context data - afterDestroy: ctx not set');
}
next();
};
Product.afterRemote('**', function(remotingCtx, unused, next) {
ctx = loopback.getCurrentContext();
if (ctx) {
console.log('context data - afterRemote:', ctx.get('data'));
} else {
console.log('context data - afterRemote: ctx not set');
}
next();
});
app.dataSource('db', { connector: 'memory' });
app.dataSource('mongox', {
"host": "localhost",
"port": 27017,
"database": "api",
"username": "root",
"password": "123456",
"name": "mongodb",
"connector": "mongodb"
});
app.model(Product, { dataSource: 'mongox' });
//app.model(Product, { dataSource: 'db' }); I did some test with your example and followings are testing result: loopback v2.8.2 with "memory" datasource context data - afterInitialize : test
context data - beforeValidate : test
context data - afterValidate: test
context data - beforeCreate: test
context data - beforeSave : test
context data - afterSave: test
context data - afterCreate: test
context data - afterRemote test with "mongox" datasource context data - afterInitialize : test
context data - beforeValidate : test
context data - afterValidate: test
context data - beforeCreate: test
context data - beforeSave : test
context data - afterSave: ctx not set
context data - afterCreate: ctx not set
context data - afterRemote: ctx not set testing result after applied the patch proposed by Ichenay at this comment, everything is fine now context data - afterInitialize : test
context data - beforeValidate : test
context data - afterValidate: test
context data - beforeCreate: test
context data - beforeSave : test
context data - afterSave: test
context data - afterCreate: test
context data - afterRemote: test so, it seems you need to consider and apply his patch soon ^^ |
@projectxmaker thank you for an extensive test. I have updated the title of this issue to reflect the problem. Could you please wrap up @Ichenay's patch and send it as a proper pull request yourself? |
The fix is available as of |
A follow-up for #766.
I tested on https://github.com/strongloop/loopback-example-app
Step to create bug like this:
My code:
and my log:
The text was updated successfully, but these errors were encountered: