Reputation: 397
I have been working on porting a basic Express based app to Meteor. The excellent post Is there an easy way to convert an express app to meteor? was a great start, using a waiter function to wrap iron-router routes that Meteor expects into req / res that Express likes.
However, I've hit a bug that I'm stuck on. I'm not able to get Meteor to pass the res.render object over to my handlebars templating engine.
For example:
main.js
app.get('/complex', function(req, res){
var data = {
name: 'Gorilla',
address: {
streetName: 'Broadway',
streetNumber: '721',
floor: 4,
addressType: {
typeName: 'residential'
}
}
};
res.render('complex', data);
});
When the /complex route is called via iron-router, it is routed to the function res.render below
/** create an sync version for meteor */
waiter = function(foo, req, res) {
var waiter_aux = Meteor._wrapAsync(function(foo, req, res, callback) {
res.set = function(header, value) {
res.setHeader(header, value);
};
res.send = function(codeorhtml, html) {
if (html) {
// two arguments provided, treat as described
res.statusCode = codeorhtml;
} else {
// no code, just html
html = codeorhtml;
}
callback(null, html);
};
res.render = function(name, data, callback) {
callback = callback || function(err, html) {
res.send(html);
};
console.log(name); // complex
console.log(data); // Gorilla...
var html = Handlebars.templates[name](data); // THIS ERRORS OUT
html = JSON.stringify(name) + " " + JSON.stringify(data) // WORKS
callback(null, html);
};
...
In the message above, the compiler errors out saying that Handlebars is undefined.
W20140828-22:47:49.439(-7)? (STDERR) TypeError: Cannot call method 'complex' of undefined
W20140828-22:47:49.439(-7)? (STDERR) at ServerResponse.res.render (app/server/myapp.js:57:50)
W20140828-22:47:49.440(-7)? (STDERR) at app/server/myapp.js:298:25
I used NPM's handlebars package to build pre-compiled templates (example below), but I haven't had any luck getting it to work correctly
(function() {
var template = Handlebars.template, templates = Handlebars.templates = Handlebars.templates || {};
templates['complex'] = template({"compiler":[6,">= 2.0.0-beta.1"],"main":function(depth0,helpers,partials,data) {
var helper, functionType="function", helperMissing=helpers.helperMissing, escapeExpression=this.escapeExpression;
return "\n<p>\nThe data that was passed to `res.render` is:\n<code>var data = {name: 'Gorilla'};</code>\n</p>\n\n<p>\nWe can display the value of <em>name</em> using <code>{{name}}</code>, which results in: <b>"
+ escapeExpression(((helper = (helper = helpers.name || (depth0 != null ? depth0.name : depth0)) != null ? helper : helperMissing),(typeof helper === functionType ? helper.call(depth0, {"name":"name","hash":{},"data":data}) : helper)))
+ "</b>.\n</p>\n";
},"useData":true});
})();
Even going the simplest route of defining a local template
query_string = "<code>var data = {name: 'Gorilla'};</code><p>{{data}}</p>"
template = Handlebars.compile(query_string)
Results in an error:
W20140828-21:51:47.126(-7)? (STDERR) TypeError: Object function exphbs(config) {
W20140828-21:51:47.128(-7)? (STDERR) return exphbs.create(config).engine;
W20140828-21:51:47.129(-7)? (STDERR) } has no method 'compile'
Any suggestions or examples about how I can successfully pass a JSON document object off to Handlebars for rendering inside Meteor/Express would be much appreciated. Ideally, I'd like to use real-time partials and not pre-compiled code for simplicity. Thanks!!!
Upvotes: 0
Views: 379
Reputation: 21364
In order for my previous answer to work, you now need to add handlebars-server:
meteor add cmather:handlebars-server
Upvotes: 0
Reputation: 53
If it's a basic app I'd suggest just starting over, reusing stuff you can. You'll add a lot of complexity by creating async wrappers to your project.
Upvotes: 1