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
Chapter 7 - Handlebars.compile is confused with partials.weather and weather.handlebars #28
Comments
This is my meadowlark.js file, I did it a little bit different from the original // NPM MODULES
var express = require('express');
// NODE MODULES
var fortune = require('./lib/fortune.js');
// EXPRESS INITIATION
var app = express();
// RESPONSE'S HEADER CONFIGURATION
// Disable sensitive information server
app.disable('x-powered-by');
// STATIC RESOURCES
app.use(express.static(__dirname + '/public'));
// ENGINES
// Set up handlebars view engine
var handlebars = require('express-handlebars').create({defaultLayout: 'main'});
app.engine('handlebars', handlebars.engine);
app.set('view engine', 'handlebars');
// PORT CONFIGURATION
app.set('port', process.env.PORT || 3000);
// FUNCTIONS
// Function to return weather data
function getWeatherData() {
return {
locations: [
{
name: 'Portland',
forecastUrl: 'http://www.wunderground.com/US/OR/Portland.html',
iconUrl: 'http://icons-ak.wxug.com/i/c/k/cloudy.gif',
weather: 'Overcast',
temp: '54.1 F (12.3 C)'
},
{
name: 'Bend',
forecastUrl: 'http://www.wunderground.com/US/OR/Bend.html',
iconUrl: 'http://icons-ak.wxug.com/i/c/k/partlycloudy.gif',
weather: 'Partly Cloudy',
temp: '55.0 F (12.8 C)'
},
{
name: 'Manzanita',
forecastUrl: 'http://www.wunderground.com/US/OR/Manzanita.html',
iconUrl: 'http://icons-ak.wxug.com/i/c/k/rain.gif',
weather: 'Light Rain',
temp: '55.0 F (12.8 C)'
}
]
};
}
// MIDDLEWARE
// Middleware to inject data into res.locals.partials
app.use(function (request, response, next) {
if (!response.locals.partials) response.locals.partials = {};
response.locals.partials.weatherData = getWeatherData();
next();
});
// Middleware to enable page testing
app.use(function (request, response, next) {
response.locals.showTests = app.get('env') !== 'production' && request.query.test === '1';
next();
});
// ROUTES
app.get('/', function (request, response) {
response.render('home');
});
app.get('/about', function (request, response) {
response.render('about', {
fortune: fortune.getFortune(),
pageTestScript: '/qa/tests_about.js'
});
});
app.get('/tours/hood-river', function (request, response) {
response.render('tours/hood_river');
});
app.get('/tours/oregon-coast', function (request, response) {
response.render('tours/oregon_coast');
});
app.get('/tours/request-group-rate', function (request, response) {
response.render('tours/request_group_rate');
});
// ERROR HANDLING
// Custom 404 page
app.use(function (request, response) {
response.status(404);
response.render('404');
});
// Custom 500 page
app.use(function (error, request, response) {
console.error(error.stack);
response.status(500);
response.render('500');
});
// SERVER CONFIGURATION
app.listen(app.get('port'), function () {
console.log('Express started on http://localhost:' + app.get('port') + '; press Ctrl-C to terminate.');
}); |
I've run into the same problem, and changing the name of the data to weatherData also fixed it. |
This is a known problem resulting from changes in the |
Hi, I got the book from O'Reilly, is that version going to be updated soon? |
I think the issue is due to the fact that the weather information has been namespaced under partials like res.locals.partials.weather Renaming the namespace from partials to something else will also fix the problem. The namespace 'partials' must be conflicting with the express-handlebars library. I changed my code to the following and it worked. app.use(function(req, res, next){
if(!res.locals.partialsData) res.locals.partialsData = {};
res.locals.partialsData.weather = getWeatherData();
next();
}); Also don't forget to change partials to partialsData in the weather.handlebars template. |
As previously mentioned, this is a problem that was caused by the 1.0 update of |
@Ikhos it has been updated |
@gustavoandrescabral thank you for posting this! |
@gustavoandrescabral thanks, fix my bug 👍 |
as this is my first experience with node and express. i was stuck with that handlebar problem, big thanks for helping me out |
I've tried the fixes and I'm still having the issue with "Error: The partial weather could not be found" I've updated the calls using weatherContext, partialsContext, and some of the suggestions of the OP. I even updated the version of express-handlebars and copied the code from the repo. |
Hi, @DanStockham, I just ran the code from the companion repo, and it works fine. Can you paste your I'm currently working on a 2nd edition of this book, and LOTS has changed in the 2-plus years this book has been on the shelves. The 2nd edition is a pretty massive overhaul. |
Here is a snippet of package.json. I'm also using Ubuntu 16.04.
|
I'm also facing same issue in Window OS. Is there any clue of this issue? "Error: The partial weather could not be found" |
Hi, @DanStockham and @simplist88, I'm afraid I was not able to reproduce your problem in either Linux or Windows...and I switched to the exact version of
I hope this helps! |
partials directory name is reserved. And the namespace 'partials' must be conflicting with the express-handlebars library. EthanRBrown/web-development-with-node-and-express#28
problem fixed. Thanks very much |
I notice that the inclusion of the partial
{{> weather}}
in the home.handlebars file is getting confused between the name of the widget weather.handlebars and the partial variable located inres.locals.partials.weather
because a saw this error on my browser:So, I decided to replace
res.locals.partials.weather
withres.locals.partials.weatherData
. Finally, the weather.handlebars file will look like this:These changes worked well for me. Hope it helps.
The text was updated successfully, but these errors were encountered: