Bonjour,
First thing’s first, this is a poorly phrased question, I’m sorry about that.
I’m using (MongoDB, Express, Ember, and Node) to build a reference engine. Pretty much everything is going smoothly until I need to read info from the database.
I have an express route (/charlist) that has:
ul
each char, i in charlist
li
p #{char.name}
It’s in Jade, but as I understand it, it’s an each statement looking for char.name in char. I took this setup directly from this tutorial, which doesn’t explain what “char” (or in the tuts’ case, ‘user’) actually refers to. But, on the Express route, it works just fine.
Writing the same statement in Ember, however, doesn’t work.
<ul>
{{#each char in charlist}}
<li>{{name}}</li>
{{else}}
Sorry, nothing here.
{{/each}}
</ul>
With that, I get the else
message. Replacing {{name}}
with {{char.name}}
does the same thing. Copying it directly, and using
<li>#{char.name}</li>
throws a “Cannot read property ‘name’ of undefined” error.
I’m not exactly sure what I need to do to get Express to hand off the data to Ember.
This is the Express router that gets the character information (ignore the weird spacing caused by the code indent here):
/* GET Character page. */
router.get('/charlist', function(req, res) {
var db = req.db;
var collection = db.get('charcollection');
collection.find({},{},function(e,docs){
res.render('charlist', {
"charlist" : docs
});
});
});
Here’s the handlebars template (in .jade):
script(type="text/x-handlebars" id='test').
<h1>Character list</h1>
<ul>
{{#each char in charlist}}
<li>#{char.name}</li>
{{else}}
Sorry, nothing here.
{{/each}}
</ul>
A friend told me to, basically, wrap the whole Express bit in a variable and call it from Ember, but neither of us know if that would actually help. Obviously, I have a database with content, and there is a name field, the Express route is functional, just need to figure out how to get it to work with Ember.
Any advice on getting this to work would be greatly valued, thanks!