-1

I need to access an array(object object) in a EJS page, my setup is displaying an array brought from database.. I've come as far as displaying the object on the page, but when accessing I'm having a hard time..

I'm using the following code:

<%  datacollection.forEach(function(data){ %>

    <h1><%=data.name%></h1>
<% } %>

but with this I am getting a error:

SyntaxError: missing ) after argument list in C:\Users\FT\Desktop\foobase-master\project\html\pages\quotes.ejs while compiling ejs

or saying that same code is not a function if I add a ')' where I think it makes sense..

Help!

EDIT: How can I loop through this object array? Already tried a for loop but leaves a blank space, no entries.. **Using this:

<ul class="quotes">
  <!-- Loop through quotes -->
  <% for(var i = 0; i < datacollection.length; i++) {%>
    <li class="quote">
      <!-- Output name from the iterated quote object -->
      <span><%= datacollection[i].name %></span>:
      <!-- Output quote from the iterated quote object -->
      <span><%= datacollection[i].quote %></span>
    </li>
  <% } %>
</ul>
LCD1
  • 19
  • 5

2 Answers2

0

In your code, you forgot to close the foreach ')'. That is why it was showing error of missing closing round bracket.

<%  
datacollection.forEach(function(data){ %>

<h1><%=data.name%></h1>
<% }); %>
  • I am getting an error with that... datacollection.forEach is not a function – LCD1 Oct 16 '22 at 17:37
  • Can you share datacollection object. What values does datacollection have? – Chintan Trivedi Oct 16 '22 at 17:41
  • a whole bunch of these: { _id: new ObjectId("634c33b22ab05cbbbf6ea598"), name: 'Francisco', nickname: 'T', email: 'inaminu8@gmail.com' }, { _id: new ObjectId("634c340fcd1993006dab3b63"), name: 'FT', nickname: 'FT', email: 'FT' }, { _id: new ObjectId("634c3496e87b256da0ab3e6d"), name: '111111', nickname: 'T', email: 'yoox3' }, All entries of me trying out the insert to DB collection – LCD1 Oct 16 '22 at 17:42
  • Check this link: https://onecompiler.com/ejs/3yk5exs24 – Chintan Trivedi Oct 16 '22 at 17:46
  • You probably not wrapping up the objects into single arrray, and iterating. That's why it is saying forEach is not a function. If you wrap it in a array, then it will work. – Chintan Trivedi Oct 16 '22 at 17:48
  • Do you know a different way to loop over the iterations of array? this one gices me a not a function error when compiling ejs... Or better, how do I wrap the array? – LCD1 Oct 16 '22 at 17:49
  • You can follow this stackoverflow answer: https://stackoverflow.com/a/3010848/17791486 – Chintan Trivedi Oct 16 '22 at 17:54
  • I have just learned that NodeJs outputs a BSON type of array, do you know how to turn to Json? – LCD1 Oct 16 '22 at 18:53
  • Mongo db stores data into BSON format. You can directly do operation on it. If not able to do, then you can use json.stringify that bson and then json.parse. This will generate JSON. – Chintan Trivedi Oct 17 '22 at 01:09
0

If you are passing the object as context from the the backend, try this:

<% context.datacollection.forEach(function(data) { %>
  <h1><%=data.name%></h1>
<% }); %>
Tyler2P
  • 2,324
  • 26
  • 22
  • 31
Goody
  • 1
  • 2