Vlad Z. Vlad Z. - 1 month ago 4x
Node.js Question

Sails.js - passing data to a view

I've recently started learning how to use Sails.js and I came across a small issue.

I have a model called Applicant, as follows:

module.exports = {

attributes: {
name: {
type: 'STRING',
required: true

email: {
type: 'STRING',
required: true



I have built the index action to return all the instances of the Applicant model:

module.exports = {
index: function(req, res) {
Applicant.findAll().done(function(err, applicants) {
apps: applicants

And this is the view that I am using:

<section id="applicants">
<h1>List of applicants</h1>

<% for (applicant in apps) { %>
<li><%= applicant.name %> <%= applicant.email %></li>
<% } %>

Still, when I load the corresponding URL, I can't see my data displayed.

I have read the Sails.js documentation, but I can't seem to find what's wrong with my code.

It would be great if someone could help me solve this problem.

Thank you!



<%= apps[applicant].name %>

It's just like for loop works. applicant is an index in apps array.

Edit: Better way is to use javascript array's built in method forEach, because this way you avoid cycling through elements, inherited from apps's type (for example, elements, assigned like this: apps.__proto__.foo = {name: "bar"} or Array.prototype.foo2 = {name: "pi222"}).

<% apps.forEach(function(applicant)  { %>
    <li><%= applicant.name %> <%= applicant.email %></li>
<% }); %>

P.S. Of course this javascript's method (forEach) wouldn't work in IE <= 8(if you consider to use it in browser). But we are inside NodeJs, which is built on top of V8 engine. So this will work, anyway.