最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Sails.js - passing data to a view - Stack Overflow

programmeradmin3浏览0评论

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) {
            res.view({
                apps: applicants
            });
        });
    }
};

And this is the view that I am using:

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

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

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!

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) {
            res.view({
                apps: applicants
            });
        });
    }
};

And this is the view that I am using:

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

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

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!

Share Improve this question asked Aug 14, 2013 at 10:59 vladzamvladzam 5,9086 gold badges32 silver badges36 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 10

Try

<%= 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.

发布评论

评论列表(0)

  1. 暂无评论