1

Please look at the code and solve this problem

enter image description here

In the Image only One data is rendering at a time but I want to render more than one data from the database. In the mongo database there are three entries but when I take the data from the database only one data is rendering.

Below is the code I used to render data from the database

enter image description here

enter image description here

1

1 Answer 1

1

You need to pass the array itself to the template instead of calling res.render for each entry:

const contacts = results.map(contact => ({
    Fname: contact.fname,
    Lname: contact.lname,
    Email: contact.email,
    Phone: contact.phoneNumber
}));

res.render("saved", {
    contacts
});

Then in your template use forEach to iterate the data and render each entry, e.g.:

<ul>
 <% contacts.forEach(function(contact) { %>
    <li> First Name:<%= contact.Fname %> Last Name:<%= contact.Lname%></li>
 <% }); %> 
</ul>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the Answer It worked. Really I've been wondering for so long to find a solution for this. Once again thank you so much😊.

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.