简体   繁体   English

如何将 json 嵌套数组渲染到车把视图

[英]How to render json nested array to handlebar view

I have a collection of docs in MongoDB as below:我在 MongoDB 中有一组文档,如下所示:

    {
    "_id" : ObjectId("5a8706973e4306202c424122"),
    "title" : "how to make a robot?",
    "description" : "arif",
    "createdBy" : ObjectId("5a71a0ebc252020d4c127911"),
    "allLearningGoals" : "my third learning goals, my fourth learning goals",
    "resources_upload" : "{\"data\":[]}",
    "participants" : "[{\"email\":\"sur@gmail.com\"},{\"email\":\"arif@pbl.de\"},{\"email\":\"mac@gmail.com\"}]",
    "createdAt" : ISODate("2018-02-24T10:15:42.548Z"),
    "__v" : 0
}

I want to display my each participants in a table and also use them for sending email to all notifications.我想在表格中显示我的每个参与者,并使用它们向所有通知发送电子邮件。 Data is coming using mongo findById and i can see it in console as:数据来自使用 mongo findById ,我可以在控制台中看到它:

console.log(data.project); // this is allright

So in handlebar i can see other data as:所以在车把中我可以看到其他数据:

    {{#if data.project}}
{{ data.project.title }}
{{ data.project.description}}

But for participants if i use this i see whole array as:但是对于参与者来说,如果我使用它,我会将整个数组视为:

{{ data.project.participants}} 

it shows:表明:

[{\"email\":\"sur@gmail.com\"},{\"email\":\"arif@pbl.de\"},{\"email\":\"mac@gmail.com\"}]

How can i display like that:我怎么能这样显示:

email: sur@gmail.com 

I tried this:我试过这个:

    {{#if data.project}}
<!-- added student list-->
<div class="form-group">
    <div class="form-row">
        <div class="col-md-12">
            <table class="table table-bordered">
                <thead>
                <tr>
                    <th scope="col">Name</th>
                    <th scope="col">Email</th>
                </tr>
                </thead>
                <tbody>
                {{#each participants}}
                    <tr>
                        <td>
                            {{ this }}
                        </td>
                        <td>
                            test
                        </td>
                    </tr>
                {{/each}}
                </tbody>
            </table>
        </div>
    </div>
    <!-- added student list end -->
</div>
{{/if}}

not working...please give some nice idea.不工作...请给出一些好主意。 thanks谢谢

Try this, I guess you try to access your data the wrong way试试这个,我猜你试图以错误的方式访问你的数据

<tbody>
    {{#each data.project.participants }}
     <tr>
         <td>
             <!-- -->
         </td>
         <td>
             {{ this }}
         </td>
     </tr>
   {{/each}}
</tbody>

Take a look at handle bars iteration section : https://handlebarsjs.com/builtin_helpers.html#iteration看看把手迭代部分: https : //handlebarsjs.com/builtin_helpers.html#iteration

When I retrieve this data again, I have to parse it in different participants array:当我再次检索这些数据时,我必须在不同的参与者数组中解析它:

locals.data = {
        project: [],
        participants: [],
    };    
Project.model.findById(req.params.id).exec(function (err, result) {
                    locals.data.project = result;
                    console.log(result.participants);
                    var obj = JSON.parse(result.participants);
                    console.log(obj[0]);
                    locals.data.participants = obj;
                });

then in view of handlebar i can see my data as:然后鉴于车把,我可以将我的数据视为:

<table class="table table-bordered">
            <thead>
            <tr>
                <th scope="col">Name</th>
                <th scope="col">Email</th>
            </tr>
            </thead>
            <tbody>
            {{#each data.participants}}
                <tr>
                    <td>
                        {{email}}
                    </td>
                    <td>
                        test
                    </td>
                </tr>
            {{/each}}
            </tbody>
        </table>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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