javascript - Jade when passing object/array from router get error -
javascript - Jade when passing object/array from router get error -
i'm exploring jade view engine in node , spent 3 hours not beingness able pass object (the response database) jade view without getting error.
from router all
array objects:
var router = express.router(); router.get('/all', function(req, res){ db.findall().then(function(all){ res.render("index", { creators: }); }); });
jade view:
doctype html html head title jade template body .comment_list each el in creators p= el.creator
as see it's pretty simple. i've seen more 20 examples of using jade , same , error:
> typeerror: index.jade:7 5| body 6| .comment_list > 7| each el in creators 8| p= el.creator cannot read property 'length' of undefined
until tried check if(typeof(creators) != "undefined")
before each el in creators
, guess what... magic happened. error disappeared.
i'm writing post reference people struggling same issuse , i'd inquire reason error , why in documentation of jade mentioned 1 should create such check undefined before iterating through collection?
it mutual pattern in jade check variable's existence before trying operate on it.
from jade documentation on iterations:
the object or array iterate on plain javascript can variable or result of function phone call or else.
- var values = []; ul each val in values.length ? values : ['there no values'] li= val
under hood, jade using standard javascript when phone call each
.
so trying phone call each
on doesn't exist trying do:
for(var i=0; < undefined.length; i++) { ... }
which errors because length
isn't real property of undefined
.
totally understand frustration, dealing undefined info pains lot of people in javascript.
javascript node.js express jade
Comments
Post a Comment