Reputation: 153
Why doesn't this work?
music.js
var files = fs.readdirSync('public/mymusic');
view.render('music', {mydata: files});
music.jade
script(type='text/javascript').
for (var i=0; i<mydata.length; i++) {
console.log(mydata[i]);
}
ReferenceError: mydata is not defined
This link seems to say I need to stringify and then parse the object, but then I no longer have the original array. how to pass an array of objects to jade template?
Upvotes: 0
Views: 735
Reputation: 5265
Are you sure you want to output a block of Javascript code to be ran client-side?
If that's so, you have to define the variable in the client-side Javascript:
script(type="text/javascript").
var mydata = !{JSON.stringify(mydata)}
for (var i=0; i<mydata.length; i++) {
console.log(mydata[i])
}
The data you pass to your Jade view engine is not shared between client-side & server-side code.
Upvotes: 1