Handlebars - {{#each}} over an array with null values - is it correct that it bombs?

4,547 views
Skip to first unread message

Daniel Dornhardt

unread,
Mar 5, 2013, 6:35:59 PM3/5/13
to meteo...@googlegroups.com
Hello,

i'm pretty new to meteor.js, so 1st: Thank you to the devs, this is more awesome than awesome!

I'm trying to get my feet wet and I just stumbled over this issue:

When I try to {{#each}} over an array containing a null value I get the following errors:

In the browser console:

Uncaught TypeError: Cannot read property '_id' of null
Exception from Meteor.flush: TypeError: Cannot call method 'firstNode' of undefined

Seeing that the handlebars documentation states that {{#if }} can be used to test against null, I would have expected an array to be able to contain null values...

Or am I not getting something here?

Is it a handlebars thing or something meteor-Specific?

David Ascher

unread,
Mar 5, 2013, 7:52:31 PM3/5/13
to meteo...@googlegroups.com
Hard to know without seeing more code, but that looks like you're iterating over an array of nulls, and then looking for the _id property of the thing in the array.  So likely your helper or template variable needs to check that its input isn't null.


--
You received this message because you are subscribed to the Google Groups "meteor-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to meteor-talk...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Daniel Dornhardt

unread,
Mar 6, 2013, 8:55:45 AM3/6/13
to meteo...@googlegroups.com
Hello,

yes, I can work around the issue now that I know it exists, but it might be a bug or an inconsistency introduced by meteor maybe?

This is the minimal example:

Template:

<template name="example">
    <ul>
        {{#each exampleList}}
            <li>{{this}}</li>
        {{/each}}
    </ul>
</template>

JS:

Works:

Template.example.exampleList = function() {
    return ['one', 'two', 'three'];
};


Breaks:

Template.example.exampleList = function() {
    return ['one', 'two', 'three', null];
};

I just wouldn't expect my Program to fail because of a null value in an array?

Thanks and best wishes

Daniel

Daniel Dornhardt

unread,
Mar 6, 2013, 9:15:32 AM3/6/13
to meteo...@googlegroups.com
Hello once more,

it seems to work with handlebars alone: http://jsfiddle.net/mvdxn/ (Even though that uses a slightly older version of handlebars, my github-fu isn't too good).

My guess is that meteor is trying to look for bound data in the data structure somehow, and that doesn't work with null properties somehow?

Just curious

Daniel

Chris Mather

unread,
Mar 6, 2013, 2:00:11 PM3/6/13
to meteo...@googlegroups.com
Hi Daniel,

The reason for this can be found on line 38 of packages/handlebars/evaluate.js where the Handlebars 'each' method is implemented. For each item of your array, Meteor is creating a branch label Spark annotation using the "_id" property of the item, the item itself, or a Spark.UNIQUE_LABEL. This is so that Meteor can find the row later if/when the data changes (in a reactive data source, not a plain array). I probably should have done one more Spark screencast on labels but decided to skip it for now.

I haven't completely thought through the implications, but the 'each' code should probably be guarding against a null item in the array but it currently isn't. So that's why you're seeing that exception.

Chris


--
You received this message because you are subscribed to the Google Groups "meteor-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to meteor-talk...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
(m) 703-282-8567

Chris Mather

unread,
Mar 6, 2013, 2:12:56 PM3/6/13
to meteo...@googlegroups.com
If you're curious, I forgot to mention the reactive implementation of 'each' is in packages/templating/deftemplate.js on line 21. But same basic issue - not guarding against a null item.

C
--
(m) 703-282-8567

Ken Yee

unread,
Mar 6, 2013, 2:26:57 PM3/6/13
to meteo...@googlegroups.com


On Wednesday, March 6, 2013 2:00:11 PM UTC-5, Chris Mather wrote:
I haven't completely thought through the implications, but the 'each' code should probably be guarding against a null item in the array but it currently isn't. So that's why you're seeing that exception.

This was something I hit a week or two back as well...not very intuitive, so IMHO, it's a bug that should be fixed.
Most other templating systems treat unknowns/nulls as empty strings.  In Meteor's templates, the entire template doesn't render :-P
 

David Glasser

unread,
Mar 11, 2013, 9:22:35 PM3/11/13
to meteo...@googlegroups.com
This is worth filing as a bug on GitHub!


--

Daniel Dornhardt

unread,
Mar 12, 2013, 2:58:07 PM3/12/13
to meteo...@googlegroups.com
Hello,

i created a ticket: https://github.com/meteor/meteor/issues/801 - I hope that's alright as it is.

Thanks!
Reply all
Reply to author
Forward
0 new messages