简体   繁体   中英

Object values are not displayed when I use EJS templating engine

I have a very simple EJS setup:

staff

<ul>
<% staffs.all.Items.forEach(function(staff){%>
  <li><%= JSON.stringify(staff) %></li>
<% }); %> 
</ul>

<ul>
<% staffs.all.Items.forEach(function(staff){%>
   <li><%= staff.first_name %></li>
<% }); %>
</ul>

The output I get is:

staff
  - {"first_name":"John","staff_id":"324b2808-9f2b-4967-a27e-6f140d29ffa3","last_name":"Doe"}
  - {"first_name":"Mike","staff_id":"031c04d6-cd4a-4041-a20d-fc2ae4fbb23f","last_name":"Gates"}

  - 
  -

Why is nothing displayed in the second list? It should be:

  - John
  - Mike 

After having the same problem using the underscore templating engine. I figured out that the problem is actually in retrieving the data from DynamoDB.

Now if i do this:

app.Staff.getStaffs = function() {
    var deferred = q.defer()
    app.Staff.scan().exec(function(err, data) {
        if (err){
            deferred.reject(err);
        }else{
            deferred.resolve({
                all: JSON.parse(JSON.stringify(data))
            });
        }
    });
    return deferred.promise
}

Instead of this:

app.Staff.getStaffs = function() {
    var deferred = q.defer()
    app.Staff.scan().exec(function(err, data) {
        if (err){
            deferred.reject(err);
        }else{
            deferred.resolve({
                all: data
            });
        }
    });
    return deferred.promise
}

I get the values. Probably not the most optimal solution but it will do.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM