Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
121 views
in Technique[技术] by (71.8m points)

node.js - How to use the data passed in ejs twice

I'm doing a project with node js and i need to display some data that i fetched from my database in an ejs file. The second time I try to display the data it is empty (the datalist I'm working with has empty fields). How can i do it?

The part of my ejs file where i use the data more than once:

<body>
<%- include('../partials/navbarHorizontal.ejs') %>

<div class="container justify-content-center my-4">
  <div class="row shadow-sm my-4">
    <div class="col m-4">
      <form action="/dekanat" method="POST">
        <div>P?rdit?soni dekanatin</div>

        <div class="row">
          <div class="row">
            <label class="small gray" for="dekan">Dekani</label>
            <div>
              <input list="dekan_list" name="dekan" id="dekan" class="inputText"/>
              <datalist id="dekan_list">

              //The first time i use the data

                <%pedagoge.forEach(element => {%>
                <option
                  value="<%=element.emer_ped%> <%=element.mbiemer_ped%>"
                  data-id="<%=element.id_ped%>"
                ></option>
                <%});%>
              </datalist>
            </div>
          </div>
          <div class="col">
            <label class="small gray" for="zvDek1">Zv. Dekani</label>
            <div>
              <input list="zvDek1_list" name="zvDek1" id="zvDek1" class="inputText" />
              <datalist id="zvDek1_list">

              //The second time i use the data

                <% pedagoge.forEach(element =>{%>
                <option
                  value="<%element.emer_ped%> <%element.mbiemer_ped%>"
                  data-id="<%element.id_ped%>"
                ></option>
                <%});%>
              </datalist>
            </div>
          </div>

          <div class="col">
            <label class="small gray" for="zvDek2">Zv. Dekani</label>
            <div>
              <input list="zvDek2_list" name="zvDek2" id="zvDek2" class="inputText"/>

              <datalist id="zvDek2_list">

              //The third time i use the data

                <% pedagoge.forEach(element =>{%>
                <option
                  value="<%element.emer_ped%> <%element.mbiemer_ped%>"
                  data-id="<%element.id_ped%>"
                ></option>
                <%});%>
              </datalist>
            </div>
          </div>
        </div>

        <div class="row mt-3 text-center">
          <div class="col-4">
            <input type="submit" value="Ruaj" class="rounded-pill buton" id="saveDek" />
          </div>

          <div class="col-4">
            <input type="reset" value="Fshi" class="rounded-pill buton" id="fshi"/>
          </div>
        </div>
      </form>
    </div>
  </div>
</div>

The file where i render it is:

module.exports = {
dashboard: (req, res) => {
    alumniModel.display((resultAlumni) => {
      certificateModel.display((resultCertificate) => {
        docentModel.all((resultAllDocents) => {
          docentModel.notInDepartment((resultNotDep) => {
            departamentModel.all((resultDep) => {
              res.render("dashboard/dashboard.ejs", {
                eksperienca: resultAlumni,
                vertetime: resultCertificate,
                pedagoge: resultAllDocents,
                jashtem: resultNotDep,
                departament: resultDep,
              });
            });
          });
        });
      });
    });
  },

I know it is very nestled but i am new to to node js and i didnt know how to do it differenty.

The html code generated:

<div class="row shadow-sm my-4">
        <div class="col m-4">
          <form action="/dekanat" method="POST">
            <div>P?rdit?soni dekanatin</div>

            <div class="row">
              <div class="row">
                <label class="small gray" for="dekan">Dekani</label>
                <div>
                  <input list="dekan_list" name="dekan" id="dekan" class="inputText"/>

                  //The first time 
                  <datalist id="dekan_list">
                    <option value="John Doe" data-id="1" ></option>
                    <option value="John Doe2" data-id="2" ></option>
                  </datalist>

                </div>
              </div>
              <div class="col">
                <label class="small gray" for="zvDek1">Zv. Dekani</label>
                <div>
                  <input list="zvDek1_list" name="zvDek1" id="zvDek1" class="inputText" />

                  //The second time 
                  <datalist id="zvDek1_list"> 
                    <option value=" " data-id="" ></option> 
                    <option value=" " data-id="" ></option>
                  </datalist>
                </div>
              </div>

              <div class="col">
                <label class="small gray" for="zvDek2">Zv. Dekani</label>
                <div>
                  <input list="zvDek2_list" name="zvDek2" id="zvDek2" class="inputText" />

                  //The third time 
                  <datalist id="zvDek2_list"> 
                    <option value=" " data-id="" ></option> 
                    <option value=" " data-id="" ></option> 
                  </datalist>
                </div>
              </div>
            </div>
question from:https://stackoverflow.com/questions/65866650/how-to-use-the-data-passed-in-ejs-twice

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Your first one shows this syntax:

value="<%=element.emer_ped%> <%=element.mbiemer_ped%>"

Your second and third ones use a slightly different syntax:

value="<%element.emer_ped%> <%element.mbiemer_ped%>"
         ^                    ^

Notice the missing = where the ^ is pointing. I don't personally know ejs syntax, but this looks like the one difference between the version that works and the one that doesn't.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...