将函数值显示到现有的 EJS 模板上
我需要从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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
假设您的
ejs
文件名为calculator.ejs
并且您已在主文件中添加了ejs
中间件和ejs
> 设置。在端点中,当您发送响应时,您可以像这样发送:
您基本上渲染该文件并向其发送值,您可以在其中使用的值
在您的
calculator.ejs
中,您现在可以使用:Supposing your
ejs
file is namedcalculator.ejs
and that you added already in your main file theejs
middleware andejs
setup.In your endpoint, when you send the response, you can send it like this:
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: