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

Question: Getting another document from a hook #75

Closed
0xef53 opened this issue Dec 31, 2016 · 2 comments
Closed

Question: Getting another document from a hook #75

0xef53 opened this issue Dec 31, 2016 · 2 comments

Comments

@0xef53
Copy link

0xef53 commented Dec 31, 2016

Hello there.

I would like to ask your advice in the following task.

Before adding a new document to a MongoDB (via POST method) I need to calculate some of its fields' values. In order to do that I need to get another two documents from another collection (all collections are handled by the same app).

I seems to me that InsertEventHandler hook will come in handy here. But it's not clear to me how use it properly in that task. Might it be the http.NewRequest ?

Thanks in advance.

@rs
Copy link
Owner

rs commented Jan 3, 2017

You can access the resource.Index (the router) from your handler. This part is not documented, but the resource.Index is stored in the context here: https://github.com/rs/rest-layer/blob/master/rest/handler.go#L63.

From the index you can access any other resources using GetResource for instance, and perform internal queries on it. One example of such sub-request can be found here: https://github.com/rs/rest-layer/blob/master/rest/util.go#L186.

@0xef53
Copy link
Author

0xef53 commented Jan 11, 2017

Thanks a lot !

@rs rs closed this as completed Jan 22, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants