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

Can't access the RethinkDB context set up by the Hapi plugin. #8

Closed
ghost opened this issue Apr 21, 2016 · 4 comments
Closed

Can't access the RethinkDB context set up by the Hapi plugin. #8

ghost opened this issue Apr 21, 2016 · 4 comments
Assignees

Comments

@ghost
Copy link

ghost commented Apr 21, 2016

According to the documentation, The RethinkDB library and the connection are bound to the server context inside handlers on this.rethinkdb and this.rethinkdbConn.
However, I'm not being able to access those, only using request.server.plugins etc. Is it because the context set inside a plugin with server.bind() is only available inside the plugin itself ?

My code is like this:
index.js

const plugins = [{register: require('hapi-rethinkdb'), options: {url: //url here}]

server.register(plugins, () => {
    server.route(require('./routes'))
    server.start()
}) 

routes.js:

{
   method: 'GET',
   path: '/list-of-things',
   handler: function(request, reply) {

      // I have to use it like this:
      const r = request.server.plugins['hapi-rethinkdb'].rethinkdb;
      const connection = request.server.plugins['hapi-rethinkdb'].connection;

      // Because this throws undefined:
      const r = this.rethinkdb;
      const connection = this.rethinkdbConn
   }

 }

Thank you.

@ghostbar ghostbar self-assigned this Apr 24, 2016
@ghostbar
Copy link
Owner

It's really weird; I was checking this (long time without seeing the code) and on index.js L37-40 is making the bind correctly as defined in the documentation :-/

@ghostbar
Copy link
Owner

In other words: I don't know what's wrong. Tomorrow will probably write some tests for this.

@ghost
Copy link
Author

ghost commented May 25, 2016

I was checking the documentation here and it says that

When setting context inside a plugin, the context is applied only to methods set up by the plugin.

Maybe that's why. I'm very new to Hapi js so I don't know much about how plugins work, but perhaps there's a way to bind the library and connect with a shorter syntax. I mean, it's also fine as it is now but it's a bit verbose to make reference to rethinkdb and connection like I mentioned.

@ghostbar
Copy link
Owner

You can always import all this to a single file, and make it so that file exports them. Remember when you make an assignation on javascript is just a reference and it's cached because of node, so:

var myModule = {}
function setup (server) {
  myModule = {
    r: server.plugins['hapi-rethinkdb'].rethinkdb,
    connection: server.plugins['hapi-rethinkdb'].connection
  }
}
module.exports = myModule
module.exports.setup = setup

So, you call setup where you have server available and then just import myModule and you will have there your references to the plugin.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant