Skip to content
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

Losing context data #421

Closed
pbalan opened this issue Mar 7, 2018 · 6 comments
Closed

Losing context data #421

pbalan opened this issue Mar 7, 2018 · 6 comments
Labels

Comments

@pbalan
Copy link

pbalan commented Mar 7, 2018

Description/Steps to reproduce

I wrote a loopback component and all my tests are working with memory db.
However, when I change datasource to mongodb, I do not see the context data in operation hook.

  1. clone the repo below.
  2. npm test
  3. git checkout test-mongo
  4. npm test

Link to reproduction sandbox

https://github.com/pbalan/component-reproduce-error.git

Master Branch: runs with memory connector. TEST PASS
screen shot 2018-03-07 at 20 56 27

test-mongo Branch: runs with mongodb connector. TEST FAIL
screen shot 2018-03-07 at 20 57 06

Error thrown in source

Expected result

TEST PASS

Additional information

node -e 'console.log(process.platform, process.arch, process.versions.node)': darwin x64 8.9.4
npm ls --prod --depth 0 | grep loopback loopback-connector-mongodb@3.4.1

@pbalan
Copy link
Author

pbalan commented Mar 7, 2018

@raymondfeng @bajtos @loay @dhmlau Any solution to this yet?

@bajtos
Copy link
Member

bajtos commented Mar 23, 2018

@pbalan I am afraid the link https://github.com/pbalan/component-reproduce-error.git returns 404 to me. Is your repository perhaps private? Could you please make it public or at least grant me and my colleagues read access?

@pbalan
Copy link
Author

pbalan commented Apr 20, 2018

@bajtos Sorry for the late response. I resolved this issue as it was not related to loopback context but appeared prima-facie.
I resolved the issue related to loopback-context on single model using local object previously to avoid loopback-context altogether.

However, I again stumbled on the same problem today. Am trying to get loopback context inside middleware. @matt-landers solution doesn't work for me on
loopback@3.18.2.
loopback-connector-mongodb@3.4.1

@pbalan
Copy link
Author

pbalan commented Apr 20, 2018

Previously, I had implemented a before delete and after delete operation hook through hookstate.
The solution to the above specific problem was to override remote method.

const deleteById = Model.deleteById;

Model.deleteById = (id, cb) => {
  Model.updateAll({id: id}, {
    deletedAt: new Date(),
    deletedBy: cb.accessToken.userId,
  }, cb);
};

@bajtos
Copy link
Member

bajtos commented Apr 27, 2018

@pbalan sorry, I have no idea how to reproduce the problem you are experiencing, therefore I am not able to help you.

However, I noticed a suspicious thing in your code example - you are treating cb as an options object. I think the following code should work better for you:

Model.deleteById = (id, options, cb) => {
  Model.updateAll({id: id}, {
    deletedAt: new Date(),
    deletedBy: options.accessToken.userId,
  }, cb);
};

@stale
Copy link

stale bot commented Jun 26, 2018

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.

@stale stale bot added the stale label Jun 26, 2018
@pbalan pbalan closed this as completed Jun 29, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants