将函数值显示到现有的 EJS 模板上

发布于 2025-01-21 00:25:08 字数 2873 浏览 1 评论 0原文

我需要从JS文件(indexRoutes.js)显示一个值,将EJS模板上的输入框(calculator.ejs)上的一个我做的页脚。

这是我拥有的JS函数的代码:

router.post("/calculator", (req, res) => {
  let ethAddress = req.body.ethereumAddress;
/*BEGINNING OF CALCULATION METHOD
  /* PART 1 For getting data, using node-fetch .json to convert the data into an array of objects*/
  function APIRetrieval() {
    var userTransactions = [];
    var totalValue = 0;
    fetch(
      `https://api.etherscan.io/api?module=account&action=txlist&address=${ethAddress}&startblock=0&endblock=99999999&sort=asc&apikey=${APIKey}`
    )
      .then((data) => {
        return data.json();
      })
      .then((retrievedData) => {
        // Extract just the results from the Javascript object for a Javascript object of just the transactions.
        userTransactions = retrievedData.result;

        // This loop takes all values from all previous transactions and adds them together for a number to be used in the calculation of emissions.
        for (var i = 0; i < userTransactions.length; i++) {
          // console.log(userTransactions[i].gasPrice);
          let currentValue = Number(userTransactions[i].gasPrice);
          totalValue = totalValue + currentValue;
        }
      });
  }
  APIRetrieval();
});

这是我拥有的EJS表单的代码:

<form action="/calculator" method="POST">
  <div class="form-group px-5 mt-4">
    <div class="d-flex justify-content-center">
      <label for="ethAddress">Ethereum Address</label>
    </div>
    <div class="d-flex justify-content-center mt-2">
      <input
        type="string"
        id="ethereumAddress"
        name="ethereumAddress"
        class="form-control"
        placeholder="Enter Ethereum Address"
      />
    </div>
  </div>
  <div class="d-flex justify-content-center">
    <button type="submit" class="btn btn-primary mt-4">Calculate</button>
  </div>
  <div class="d-flex justify-content-center mt-4">
    <label for="emissionValue">Total Emission Price To Offset</label>
  </div>
  <div class="d-flex justify-content-center mt-2 mb-4">
    <input
      type="string"
      id="emissionValue"
      name="emissionValue"
      class="form-control"
      placeholder=""
      value=""
      readonly
    />
  </div>
</form>

我想从JS文件中显示totalValue值,iSUSSION> sumissionValue 输入提交后请求后,POST请求是充分运作并且确实有效的,我只是不知道如何在使用nodejs和express时替换sumissionValue

我知道这不是DOM,但是,我不知道如何解决这个问题。我还试图研究jsdom,但我无法理解这种确切的情况。

这是我的文件结构: 文件结构

I need to display a value from a JS file (indexRoutes.js) into an input box on an EJS template (calculator.ejs), I have included the script via a footer I have made.

This is the code for the JS Function I have:

router.post("/calculator", (req, res) => {
  let ethAddress = req.body.ethereumAddress;
/*BEGINNING OF CALCULATION METHOD
  /* PART 1 For getting data, using node-fetch .json to convert the data into an array of objects*/
  function APIRetrieval() {
    var userTransactions = [];
    var totalValue = 0;
    fetch(
      `https://api.etherscan.io/api?module=account&action=txlist&address=${ethAddress}&startblock=0&endblock=99999999&sort=asc&apikey=${APIKey}`
    )
      .then((data) => {
        return data.json();
      })
      .then((retrievedData) => {
        // Extract just the results from the Javascript object for a Javascript object of just the transactions.
        userTransactions = retrievedData.result;

        // This loop takes all values from all previous transactions and adds them together for a number to be used in the calculation of emissions.
        for (var i = 0; i < userTransactions.length; i++) {
          // console.log(userTransactions[i].gasPrice);
          let currentValue = Number(userTransactions[i].gasPrice);
          totalValue = totalValue + currentValue;
        }
      });
  }
  APIRetrieval();
});

This is the code for the EJS Form that I have:

<form action="/calculator" method="POST">
  <div class="form-group px-5 mt-4">
    <div class="d-flex justify-content-center">
      <label for="ethAddress">Ethereum Address</label>
    </div>
    <div class="d-flex justify-content-center mt-2">
      <input
        type="string"
        id="ethereumAddress"
        name="ethereumAddress"
        class="form-control"
        placeholder="Enter Ethereum Address"
      />
    </div>
  </div>
  <div class="d-flex justify-content-center">
    <button type="submit" class="btn btn-primary mt-4">Calculate</button>
  </div>
  <div class="d-flex justify-content-center mt-4">
    <label for="emissionValue">Total Emission Price To Offset</label>
  </div>
  <div class="d-flex justify-content-center mt-2 mb-4">
    <input
      type="string"
      id="emissionValue"
      name="emissionValue"
      class="form-control"
      placeholder=""
      value=""
      readonly
    />
  </div>
</form>

I want to display the totalValue value from the JS file, inside the emissionValue input after the post request has been submitted, the post request is fully functioning and does work, I just do not know how to replace the empty value inside emissionValue whilst using NodeJS and Express.

I understand it does not come with a DOM, however, I have no idea how to tackle this. I have also tried to look into JSDom but I cannot understand this exact scenario.

This is my File Structure:
File Structure

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

丢了幸福的猪 2025-01-28 00:25:08

假设您的 ejs 文件名为 calculator.ejs 并且您已在主文件中添加了 ejs 中间件和 ejs > 设置。

在端点中,当您发送响应时,您可以像这样发送:

router.post("/calculator", (req, res) => {
  let ethAddress = req.body.ethereumAddress;
/*BEGINNING OF CALCULATION METHOD
  /* PART 1 For getting data, using node-fetch .json to convert the data into an array of objects*/
  function APIRetrieval() {
    var userTransactions = [];
    var totalValue = 0;
    fetch(
      `https://api.etherscan.io/api?module=account&action=txlist&address=${ethAddress}&startblock=0&endblock=99999999&sort=asc&apikey=${APIKey}`
    )
      .then((data) => {
        return data.json();
      })
      .then((retrievedData) => {
        // Extract just the results from the Javascript object for a Javascript object of just the transactions.
        userTransactions = retrievedData.result;

        // This loop takes all values from all previous transactions and adds them together for a number to be used in the calculation of emissions.
        for (var i = 0; i < userTransactions.length; i++) {
          // console.log(userTransactions[i].gasPrice);
          let currentValue = Number(userTransactions[i].gasPrice);
          totalValue = totalValue + currentValue;
        }
      });
      return totalValue;
  }
  const totalValue = APIRetrieval();
  res.render("calculator.ejs", {totalValue: totalValue});
});

您基本上渲染该文件并向其发送值,您可以在其中使用的值
在您的 calculator.ejs 中,您现在可以使用:

<div class="d-flex justify-content-center mt-2 mb-4">
          <input
            type="string"
            id="emissionValue"
            name="emissionValue"
            class="form-control"
            placeholder=""
            value="<%= totalValue =>"
            readonly
          />
        </div>

Supposing your ejs file is named calculator.ejs and that you added already in your main file the ejs middleware and ejs setup.



In your endpoint, when you send the response, you can send it like this:

router.post("/calculator", (req, res) => {
  let ethAddress = req.body.ethereumAddress;
/*BEGINNING OF CALCULATION METHOD
  /* PART 1 For getting data, using node-fetch .json to convert the data into an array of objects*/
  function APIRetrieval() {
    var userTransactions = [];
    var totalValue = 0;
    fetch(
      `https://api.etherscan.io/api?module=account&action=txlist&address=${ethAddress}&startblock=0&endblock=99999999&sort=asc&apikey=${APIKey}`
    )
      .then((data) => {
        return data.json();
      })
      .then((retrievedData) => {
        // Extract just the results from the Javascript object for a Javascript object of just the transactions.
        userTransactions = retrievedData.result;

        // This loop takes all values from all previous transactions and adds them together for a number to be used in the calculation of emissions.
        for (var i = 0; i < userTransactions.length; i++) {
          // console.log(userTransactions[i].gasPrice);
          let currentValue = Number(userTransactions[i].gasPrice);
          totalValue = totalValue + currentValue;
        }
      });
      return totalValue;
  }
  const totalValue = APIRetrieval();
  res.render("calculator.ejs", {totalValue: totalValue});
});

You bassically render that file and send values to it, values that you can use inside it
In your calculator.ejs you can use now:

<div class="d-flex justify-content-center mt-2 mb-4">
          <input
            type="string"
            id="emissionValue"
            name="emissionValue"
            class="form-control"
            placeholder=""
            value="<%= totalValue =>"
            readonly
          />
        </div>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文