Wednesday, 9 September 2020

Struggling to save an array from EJS to a Javascript variable

Hello, I am new to programming and am trying to code a simple quiz game based on data I have scraped and saved it into a local SQL server. I can access the local server and save the information I need in an array in JSON format. I have also successfully exported this so I can call the function in my app.js file when a get request is made. The issue I am having is once I send the data to my client-side using res.render(), I cannot save the data as a variable that I can then use to run the quiz client-side.I have tried using 'var x = <%- gameData %>;' and 'var x = <%- JSON.stringify(gameData) %>;' in my javascript file, as some people suggested in similar posts on stackoverflow, however these dont return anything when I try console.log(x).Any help would be greatly appreciated.​//app.js const express = require("express"); const ejs = require("ejs"); const bodyParser = require("body-parser"); const getCapitals=require('./country-data-access.js') const app = express(); app.set("view engine", "ejs"); app.use(bodyParser.urlencoded({extended: true})); app.use(express.static("public")); app.get("/", (req, res) => { res.render("home"); }); app.get("/capitals-quiz", async function(req, res){ var testData = await getCapitals(); res.render("capitals_game", {gameData: testData}); }); app.listen("3000", () => { console.log("runnning on port 3000"); }); ​//client.ejs Capitals Quiz

Capitals

something something something

<%=gameData%>

Submitted September 09, 2020 at 07:22PM by MalteseOstrich

No comments:

Post a Comment