-
Notifications
You must be signed in to change notification settings - Fork 21
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
MongoDB - loopback.getCurrentContext() return null #21
Comments
Same problem again in release 3.0.0 with mysql connector. in i will report some details later. |
FWIW, loopbackio/loopback-connector-mongodb#275 shows how to patch the MongoDB connector to restore current context. |
Please open a new issue to keep track of the problem with MySQL connector. |
Downgraded loopback-connector-mongodb to 1.15.1 but context is still null when passing access token. I can confirm that patch loopbackio/loopback-connector-mongodb#275 is fixing the issue but agree with @bajtos that we should not monkey-patch loopback code. However everyone of us is fighting with project deadlines and we should put together a working solution for this asap. Regarding @bajtos comment strongloop/loopback#1495 (comment) I was able to get context in middleware from
{
"initial": {
"loopback-context#per-request": {},
},
"auth": {
"loopback#token": {}
},
"auth:after": {
"./middleware/context": {}
}
}
'use strict';
const App = require('../server.js');
module.exports = function (options) {
return function context (req, res, next) {
if (
!req.accessToken ||
!req.accessToken.userId ||
!req.loopbackContext
) {
return next();
}
var currentContext = req.loopbackContext;
// ...
};
}; @bajtos could you please explain the difference in this scenario? Why is The whole context thing is a big issue and related information is spread across github, loopback docs, stackoverflow, etc making it difficult for users to understand what's going on behind the scenes and what are the risks of using current context. @bajtos maybe the context documentation can be improved and things put together? UPDATE Although context is available with |
IIRC, |
connect to strongloop/loopback-context#21 fix the wrong url link of loopback-connector-db2
connect to strongloop/loopback-context#21 replace loopback-connector-db2z to loopback-connector-db2 in db2 table
connect to strongloop/loopback-context#21 fix loopback-connector-db2 link
@Jeff-Lewis Unfortunately, your solution with |
I think that the PR loopback-connector-mongodb 275 is, by chance, a similar idea to the one demoed in this snippet (which is for |
I came up with the following idea. |
Same issue for us. Until this is fixed we are using the patch from loopbackio/loopback-connector-mongodb#275 which does resolve the issues. I have created a new branch of the mongo connector based on the latest code from master with this patch applied, incase anyone else needs it in their projects: https://github.com/fullcube/loopback-connector-mongodb/tree/fc/bind-context-275 |
In my case, I tracked an issue with the context being lost when used with the mongo connector down to the https://github.com/fullcube/loopback-component-mq package. This package uses https://github.com/arobson/rabbot which in turn uses https://github.com/cujojs/when - which causes the context to become lost across promise chains. The issue with https://github.com/cujojs/when is documented on the loopback-context readme. I have patched Rabbot so that it uses bluebird instead of when.js. see arobson/rabbot#94 Additionally I have created a new release of loopback-component-mq that uses this updated version of Rabbot. see https://github.com/fullcube/loopback-component-mq/releases/tag/v2.2.4 This resolves many of the lost context issues that I was experiencing when used in conjunction with the mongo connector. I'd suggest that people experience this issue check to see if any of your dependencies are using when.js under the hood. |
Cross-posting @matt-landers comment from strongloop/loopback#1495 (comment) as it may be useful to other people too: On node 8.9.1 with the MongoDB connector, getCurrentContext returns null if it is not set outside of the call to a model like:
However, it does work when you move it outside:
You also must use the bind option or it will return null as well. The documentation on loopback-context will have you pulling your hair out because it will not work as written. |
@matt-landers would you mind contributing a pull request to loopback-context to update the documentation with the information you found, so that the next person using loopback-context with MongoDB does not have to pull their hair out? :) |
Adding my issue here also for anyone who attempts to fix and needs a reproducible repo. |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
This issue has been closed due to continued inactivity. Thank you for your understanding. If you believe this to be in error, please contact one of the code owners, listed in the |
@dongmai commented on Wed Jul 20 2016
Hi everyone ,
Sorry for duplication, but I'm facing it again on nearly the latest version of loopback.
Here is my
pacakge.json
I've used a middleware to save current logged user data into context but it (
getCurrentContext
) always returnednull
when working with MongoDB (memory db works just fine.)Bellow is my code,
Can anyone please have a look ? Have I done something wrong ?
Thanks.
@0candy commented on Wed Jul 20 2016
Thank you for pointing out this issue. Due to the instability, there are plans to deprecate this method.
See strongloop/loopback#1676
@syuhei176 commented on Sun Jul 24 2016
I had same problem, so I use [email protected] . It work fine.
@beornharris commented on Tue Aug 02 2016
Hey, I've run across this issue as well and just wanted to add some extra info (hope it helps somewhere). I had quick look through #1676 and didnt see anything that might help me.
I initially had an earlier version of loopback running, where I had no problems calling loopback.getCurrentContext() from within a remote method. I inadvertently updated to the current version, and started seeing issues.
I have created a demo repo (from scratch and using slc commands + customisation) to help explain.
https://github.com/beornharris/authtest
The issue I am experiencing is directly related to MongoDB and access tokens, as far as I can tell. Using the repo, the steps to replicate (in api explorer) are as follows (note: requires mongo instance) :
`{ "email": "[email protected]", "password": "user1"}
and set the Access Token.
CONTEXT: null
Given this, I have a few other observations :
EDIT: @syuhei176 - I can confirm that the issue disappears with [email protected]
@dongmai commented on Wed Aug 03 2016
Just try to use
[email protected]
And it isn't working. :(@beornharris commented on Wed Aug 03 2016
@dongmai - just to confirm, to downgrade the mongo connector you did something like the following...
so it is using 1.15.1, and not minimum 1.15.1 ?
@josieusa commented on Wed Jan 04 2017
I had the same issue using the same connector.
I solved it using my own PR and Node 6.3.1 locally:
#2#11
So far, so good.
Of course, it's going to need more tests.
I'd also mention that it solved other similar issues with getCurrentContext() for me (see #1495 for examples).
Your mileage may vary
@dongmai commented on Thu Aug 04 2016
@beornharris Yes, I've downgraded the version number in package json and re-run
npm install
of course.Before downgrading it was working on local but not on remote server, and its the same after downgrading.
@evseevnn commented on Tue Oct 11 2016
Strange, after downgrading version i try use
Model.observe('before save', ...)
and i get context === null inside.
@josieusa @bajtos
P.S. when i use mongo not work
before save
because by default for mongo version >=3 in connector no findOrCreate method. So, i improve dao.js for fire this event and get this problem. Maybe this depend on usenotifyObserversOf
? because after use this function i lose context. i tried to use commit of @josieusa but this not helped@josieusa commented on Wed Jan 04 2017
@beornharris I reproduced the issue using your
authtest
repo.I'll verify my hypothesis that the issue could be caused by the
es6-promise
dependency ofmongodb
which is imported here:https://github.com/mongodb/node-mongodb-native/blob/V2.2.19/package.json#L16
This hypothesis comes from my experience. In fact, in my PR 19 of
loopback-context
I just made some experiments, and it seems like it could fix problems with otherPromise
libraries, but only if these are used inside user code, and not some 3rd party library.Sadly, I just tested that it didn't fix your issue, because that
es6-promise
is used in a 3rd party module (mongodb), and as I just said, my PR doesn't cover this case. (Also, I don't know yet if this is the root of this issue).I'm not sure if this can be fixed by user code alone.
I know that you fixed your problem by changing versions, but other people may still be looking for proper solutions.
@Jeff-Lewis commented on Wed Jan 04 2017
@josieusa try telling MongoDB to use cls-bluebird instead and then use
cls-hooked
instead of cls when initializingcls-bluebird
.In
MongoClient.connect
we can pass in thepromiseLibrary
to use. Now I'm not sure if there is an easy way to tellloopback-connector-mongodb
to use our instance ofcls-bluebird
without forking it.The text was updated successfully, but these errors were encountered: