簡體   English   中英

在車把中顯示來自 MongoDB 的數據

[英]Displaying data from MongoDB in handlebars

我有兩個.hbs,其中一個是部分文件,特定的部分包含用於顯示數據庫數據的變量。

這是 index.hbs:

<!DOCTYPE html>
<html>
<head>
    <title> PayApp </title>
    <link href="https://fonts.googleapis.com/css?family=Roboto|Rubik&display=swap" rel="stylesheet">
    <link rel="stylesheet" type="text/css" href="/css/index.css">
    <script src="https://code.jquery.com/jquery-3.4.1.min.js" type="text/javascript"></script>
    <script type="text/javascript" src="/js/index.js"></script>

</head>
<body>
    <div id="container">
        <h1> PayApp </h1>

        <div id="content">
            <form id="payment_form">
                <input type="text" name="name" id="name" class="field" placeholder="Name"> <br>
                <input type="number" name="refno" id="refno" class="field" placeholder="Reference Number"> <br>
                <input type="number" name="amount" id="amount" class="field" placeholder="Amount"> <br>
                <input type="button" name="submit" id="submit" value="SUBMIT">
            </form>

            <p id="error"></p>

            <!--
                TODO: Display all transactions here. Each transaction should be retrieved from the database and displayed through rendering `../partials/card.hbs`.
            -->
            <div id="cards">
                {{this.name}}
            </div>
        </div>
    </div>
</body>
</html>

這是部分card.hbs:

<div class="card">
    <img src="/images/icon.webp" class="icon">
    <div class="info">
        <p class="text"> {{name}} </p>
        <p class="text"> {{refno}} </p>
        <p class="text"> Php {{amount}} </p>
    </div>
    <button class="remove"> X </button>
</div>

因此,如果我閱讀正確,您正在遍歷cards數據的集合,並希望以您在下面粘貼的部分形式對每張card進行 output。 自從我玩模板以來已經有一段時間了,但我相信你需要類似的東西:

{{#each cards}}
  {{> cardPartial name=name refno=refno amount=amount}}
{{/each}}

當您的數據進入時:

{
  cards: [
    {
      name: "cardName1",
      refno: "1",
      amount: "$300"
    },
    {
      name: "cardName2",
      refno: "2",
      amount: "$200"
    },
  ]
}

嘗試類似的方法,看看如果簡化部分 output 只是為了查看數據會發生什么? 讓我知道它是否有幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM