request.server.render throws "Uncaught error: Missing views manager" in plugin
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, 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 I'm not trying to render the html in the response. I'm trying to generate the html for some other purpose.
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").
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.
I am still thinking about it.
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.
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 ).
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().