hapi icon indicating copy to clipboard operation
hapi copied to clipboard

request.server.render throws "Uncaught error: Missing views manager" in plugin

Open futurechan opened this issue 11 years ago • 9 comments

I am trying to upgrade from v6 to v8. I have a plugin that I register like so

exports.register = function (server, options, next) {

    server.views({
        engines: { jade: require('jade') },
        compileOptions: {
            pretty: true
        },
        path:  __dirname + '/views'
    });

   server.route({
        method: 'POST', 
        path: '/path/to/resource', 
        handler:resource.create, 
        config:{
            validate:{              
                payload: require('./schemas/resource')
            }
        }
    })

and in my route handler

var create = function(request,reply){

    var context = {}

    request.server.render("name_of_view", context, null, function(err, rendered, settings){

Hoek.assert(vision.manager, 'Missing views manager'); throws because manager is undefined.

I stepped into server.views({..}), and vision.manager does get created.

futurechan avatar Dec 11 '14 19:12 futurechan

@futurechan, you can try reply.view('index') instead of request.server.render. I was able to make your example work that way. Just to keep you moving forward.

arb avatar Dec 15 '14 22:12 arb

@arb I'm not trying to render the html in the response. I'm trying to generate the html for some other purpose.

futurechan avatar Dec 15 '14 22:12 futurechan

I am experiencing the same problem. I can configure a view engine in app.js, but as soon as I move this code to a plugin, I start seeing runtime error ("Cannot render view without a views manager configured").

ArnoldZokas avatar Jan 19 '15 16:01 ArnoldZokas

From the 8.4.0 API Reference:

When server.views() is called within a plugin, the views manager is only available to plugins methods.

That's irritating.

Workaround: server.expose a function addViewManager(server) for you to call from each plugin.

garthk avatar Mar 19 '15 03:03 garthk

I am still thinking about it.

hueniverse avatar Mar 24 '15 07:03 hueniverse

+1

I'm seeing this error when using with hapi-negotiator to render text/html.

realyze avatar Apr 17 '15 12:04 realyze

I see this same error as well when just trying to call reply.view('index', {message:message}); for example. I'm trying to split my app into smaller hapi plugins. Is this common to move reply methods into a plugin as well? I think this is related to the same thing @arb mentioned, but this doesn't work either.

.-Debug: internal, implementation, error 
    Error: Uncaught error: Cannot render view without a views manager configured

Is it common to do what @garthk mentioned about server.expose?

None the less I'll wait to see what @hueniverse comes up with.

tielur avatar Apr 23 '15 04:04 tielur

For what it's worth, I was seeing my error during testing only. When I was testing the plugins I was creating a new Hapi server which did not have the view engine setup ( because that was setup in the main server file ).

tielur avatar Apr 28 '15 15:04 tielur

Moved over to https://github.com/hapijs/vision/issues/39. Once released, you will need to switch over to request.render() instead. Alternatively, you can do save the views manager created by the plugin under server.realm.plugins.vision.manager and pass that reference to the request handler and use that to call render().

hueniverse avatar Aug 10 '15 17:08 hueniverse