Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 如何在当前HTML页面上使用express从res.json呈现json对象_Node.js_Express - Fatal编程技术网

Node.js 如何在当前HTML页面上使用express从res.json呈现json对象

Node.js 如何在当前HTML页面上使用express从res.json呈现json对象,node.js,express,Node.js,Express,在过去的一天左右,我一直在网上搜寻我的问题的解决方案,但我还没有找到解决方案。我不熟悉后端开发。我希望有人能在以下方面帮助我: 我想根据是否有JSON从数据库返回来呈现当前HTML页面的一部分。单击列表中的员工时,应在同一页面中添加一个额外的列,其中包含(已解析的)JSON信息 下面是我一个人走了多远 我的HTML: <div class="container"> <div class="row mt-5"> <div

在过去的一天左右,我一直在网上搜寻我的问题的解决方案,但我还没有找到解决方案。我不熟悉后端开发。我希望有人能在以下方面帮助我:

我想根据是否有JSON从数据库返回来呈现当前HTML页面的一部分。单击列表中的员工时,应在同一页面中添加一个额外的列,其中包含(已解析的)JSON信息

下面是我一个人走了多远

我的HTML:

    <div class="container">
        <div class="row mt-5">
            <div class="col-sm">
                One of three columns
            </div>
            <div class="col-sm">
                <ul class="list-group">
                    <% employees.forEach(function(employee){ %>
                    <a href="/employees/<%= employee._id %>">
                        <li class="list-group-item"> <%= employee.firstname %>
                            <%= employee.lastname %></li>
                    </a>
                    <% }); %>
                </ul>
            </div>

            <div class="col-sm">
                <!-- I want to render a card with info for the selected employee when that employee in the list is clicked  -->
            </div>
        </div>
    </div>
单击列表中的员工时返回的JSON(在浏览器中):

{
_id: "5e4423cff5a7810d1cce0e53",
firstname: "Johnny",
lastname: "Koch",
telnum: 881234567,
email: "j.koch@focus.com",
image: "https://images.unsplash.com/photo-1573007974656-b958089e9f7b?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1350&q=80",
position: "Business Analist",
__v: 0
}
正如您所看到的,我能够将正确的JSON对象发送到客户端,但我仍然无法确定从哪里开始


非常感谢您的帮助

您必须将所需的所有信息传递给您的
员工
页面,方法与当前使用
res.render(“员工/概述”、{employees:[…],foundemployees:{…})的方式相同
或者您在该页面上使用了
JavaScript
,该页面将调用返回
JSON
API
端点,然后将结果附加到
col sm
元素。感谢您的评论,我将尝试这两个选项。
{
_id: "5e4423cff5a7810d1cce0e53",
firstname: "Johnny",
lastname: "Koch",
telnum: 881234567,
email: "j.koch@focus.com",
image: "https://images.unsplash.com/photo-1573007974656-b958089e9f7b?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1350&q=80",
position: "Business Analist",
__v: 0
}