0

I am trying to return a value of an objects that are inside of an array.

const bugSchema = new Schema({
title: {
    type: String,
    required: true
},
 comments:[
    {
        user:{
            type: String,
            required: true
        },
        content: {
            type: String,
            required: true
        }
    }
]

});

I have tried the following function but it does not return anything.

    <% for (bug of bugs) { %>
                        <ion-card>
                            <ion-card-header>
                                <ion-card-title><%= bug.comments.filter(function (value) { value.content }) %></ion-card-title>

                            </ion-card-header>
                            <ion-card-content>

                            </ion-card-content>
                        </ion-card>

                    <% } %>

However I can return an array but it returns the entire objects rather than value of each one.

          <% for (bug of bugs) { %>
                        <ion-card>
                            <ion-card-header>
                                <ion-card-title><%= bug.comments %></ion-card-title>

                            </ion-card-header>
                            <ion-card-content>

                            </ion-card-content>
                        </ion-card>

                    <% } %>

The screenshot below shows how the object looks like

enter image description here

What I am expecting to achieve is to create a ion-card populated with comment's content.

1
  • can you share sample bug object? Commented Nov 28, 2019 at 15:01

2 Answers 2

1

I think this shall work:

<% for (bug of bugs) { %>
  <% for (comment of bug.comments) { %>
    <ion-card>
      <ion-card-header>
        <ion-card-title>
          <%= comment.user %>
        </ion-card-title>
      </ion-card-header>
      <ion-card-content>
        <%= comment.content %>
      </ion-card-content>
    </ion-card>
  <% } %>
<% } %>
Sign up to request clarification or add additional context in comments.

Comments

0
<% bugs.forEach(function(elem) { %>
    <ion-card>
        <ion-card-header>
            <ion-card-title><%= elem.content %></ion-card-title>

        </ion-card-header>
        <ion-card-content>

        </ion-card-content>
    </ion-card>
<% }) %>

This should work, I haven't tested it. Keep in mind that Array.filter returns an array with elements that pass a condition. If you want to iterate all the elements do Array.forEach.

EDIT

Working example:

const ejs = require("ejs");

let post = {
    _id: 123,
    title: "Comments",
    description: "...",
    date: "...",
    time: 15.2,
    assignedTo: "Elon Musk",
    status: "open",
    comments: [
        {
            user: "Elon",
            content: "Test1"
        },
        {
            user: "Elon2",
            content: "Test2"
        }
    ]
};

let render = ejs.render(
    "<% bugs.forEach(function(elem) { %><ion-card><ion-card-header><ion-card-title><%= elem.content %></ion-card-title></ion-card-header><ion-card-content></ion-card-content></ion-card><% }) %>",
    { bugs: post.comments }
);

console.log(render);

//render -> <ion-card><ion-card-header><ion-card-title>Test1</ion-card-title></ion-card-header><ion-card-content></ion-card-content></ion-card><ion-card><ion-card-header><ion-card-title>Test2</ion-card-title></ion-card-header><ion-card-content></ion-card-content></ion-card>

bugs should be post.comments.

4 Comments

Actually it does not. Returns an error "Cannot read property "for Each" of undefined"
@HalfMartianHalfHuman How are you populating the variable "bugs"? It should be an array of comments.
I have posted a sample bug object populated with comments and the object schema. You can have a look on that
@HalfMartianHalfHuman Provided working example. However you name the variable (I named it as "bugs") should point to post.comments / your object.

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.