Permalink
Browse files

Check for the existence of the 'categories' array, instead of assumin…

…g it's there.

This correct Wheat from throwing exceptions when an article was written with no categories.
  • Loading branch information...
1 parent c994561 commit 2600dae6115025ac38bab605ca489e60cf0d5ab6 @TooTallNate TooTallNate committed with Nov 2, 2010
Showing with 11 additions and 9 deletions.
  1. +8 −6 lib/wheat/data.js
  2. +3 −3 lib/wheat/renderers.js
View
14 lib/wheat/data.js
@@ -300,12 +300,14 @@ var Data = module.exports = {
function processCategories(err, articles) {
if (err) { callback(err); return; }
var categories = articles.reduce(function (start, element) {
- element.categories.forEach(function(category){
- if(start.indexOf(category) == -1){
- start = start.concat(category);
- }
- });
- return start;
+ if (element && element.categories) {
+ element.categories.forEach(function(category) {
+ if(start.indexOf(category) == -1) {
+ start = start.concat(category);
+ }
+ });
+ }
+ return start;
}, []);
return categories;
},
View
6 lib/wheat/renderers.js
@@ -207,9 +207,9 @@ var Renderers = module.exports = {
function applyTemplate(err, articles, description, categories) {
if (err) { callback(err); return; }
- var articlesForCategory = articles.reduce(function (start, element){
- return element.categories.indexOf(category) >= 0 ? start.concat(element) : start;
- }, []);
+ var articlesForCategory = articles.reduce(function (start, element){
+ return element.categories && element.categories.indexOf(category) >= 0 ? start.concat(element) : start;
+ }, []);
Tools.render("index", {
articles: articlesForCategory,

1 comment on commit 2600dae

@acmarques

Hi, I noticed this issue yesterday, but I did a slightly different solution:

http://github.com/acmarques/wheat/commit/cb848e687a1687616d70a4cbb0969cf650b68765

When reading the article, if there are no categories declared, I simply return an empty array. The problem is that I was returning undefined...

What do you think guys?

Please sign in to comment.