9

I have a results variable that is an array of objects. I carry the results variable from my javascript file to my main route file. I am trying to render my page to display lists of each object in my template ejs file. I am able to list everything fine, but the lists are coming out as [object object] instead of the actual words that are in the objects. How do I get this to display as strings in my template file?

This is my route file:

app.get('/announcement', function(req,res){
        var getAnnouncements = require('../public/javascripts/announcement.js'); //Load the module, get the name of the script file

        //define the functions here
        var onSpreadsheetSuccess = function (results) { //result is announcementArray

            //add results list to template);
            res.render('announcement', {title: "Announcement page", results: results});

        }

        getAnnouncements.loadSheet(onSpreadsheetError, onSpreadsheetSuccess); //call the function from script with the parameters passed

})

This is what I am doing in my template ejs file:

<ul>
    <% results.forEach(function(result){ %>
        <li><%= result %></li>
    <%  }); %>
</ul>
1
  • Did you solve this? I am facing similar issue!!! I am getting array not defined error while running Commented May 21, 2020 at 2:38

6 Answers 6

9
<ul>
    <% for(var i=0; i<results.length; i++) { %>
        <li>
            <%= results[i] %>
        </li>
    <% } %>
</ul>
Sign up to request clarification or add additional context in comments.

1 Comment

This is a nice solution, but how would you show just one field of that object instead of showing the entire object? I am struggeling with that.
9

This will show list of id of your results, just change _id by your property of objects as you want to show.

 <ul> 
  <% results.map((result)=>{ %>
    <li>
        <%= result._id %>
    </li>
  <% }) %>
 </ul>

Comments

6

My answer is as follows. I changed one line from the answer by other person.

<ul>
<%for (var result in results){%>
  <li><%=result%>:<%=results[result]%></li>    
  <%}%>
</ul>

Comments

1

You should be fine with this:

<ul>
<% for(let i = 0; i < results.length; i++){ %>
    <li><%= results[i].title %></li>
<% } %>
</ul

or when you rather like to use the forEach:

<ul>
<% results.forEach( function (result) { %>
    <li><%= result.title %></li>
<% }) %>
</ul

the object you push(ed) into your array:

const result = {
 title: anyTitle,
 input: anyInput
}

Comments

0

With modern JS it can be simplified to

<ul>
    <% for (var result of results) { %>
        <li>
            <%= result %>
        </li>
    <% } %>
</ul>

Comments

-2

Try this:

<ul>
     <%for (var result in results){%>
                <li><%=result%></li>    
        <%}%>
</ul>

1 Comment

This gave me the same result.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.