I am using EJS and Express to build a web application. It doesn't display the data on initial submit but it shows when the button is pressed again. If I were to log each variable, it does print proper data on the initial submission. However, it doesn't populate on the web page until the button is pressed twice. Any help will be great!
const express = require("express");
const bodyParser = require("body-parser");
const yahoo = require('yahoo-financial-data');
const app = express();
let qty = 0;
let exDiv = "9/13/2020";
let tick = "";
const trackers = [];
app.use(bodyParser.urlencoded({ extended: true }));
app.set('view engine', 'ejs');
app.get("/", function(req, res){
res.render("home",{trackers:trackers});
});
app.post("/", function(req, res){
tick = req.body.ticker;
yahoo.price(tick, function (err, price) {
yahoo.dividendYield(tick, function (err, dividend) {
qty = req.body.qty;
total = qty * price * dividend;
rowNum = rowNum + 1;
const body = {
tick: tick,
exDiv: exDiv,
dividend: dividend,
qty: qty,
total: total
};
trackers.push(body);
});
});
res.redirect("/");
});
<!DOCTYPE html>
<html lang="en">
<body>
<h1>Test</h1>
<form action="/" method="post">
<input type="text" name="ticker" value="AAPL">
<input type="number" name="qty" value="10">
<button type="submit">Find</button>
</form>
</body>
</html>
Aucun commentaire:
Enregistrer un commentaire