I'm new to programming, I'm trying to build a web app using nodejs/expressjs. so basically on the post route, after some processing on backend it gives an array of object in result variable, I want to use this result variable data and construct a table in html file and send this table.html file as a response to client
the problem I'm encountering is how can I use this result variable to add element of table in html file dynamically (since array size changes everytime )
approach i was using
- i created table.html and table.js that is linked with same html file.. but when table.html is send as a response that html file and table.js file is rendered on client side and not able to pass the result value that i get from server to that table.js file ( i get a document not defined error)
- i tried to use EJS template .. how to create a function that used ejs variable to loop through array data and show table.. i'm unable to do
if anyone can help me to show result=[{name:'james', id:23},{name:'joe',id:35}]
to html file as response
main.js
app.post('/dataTbale',(req,res)=>{
// after some processing i get array of obj that i store in result variable
const result=[{name:'james', id:23},{name:'joe',id:35}]
res.send('table.html)
})
table.html
<html>
<body>
<h2>table</h2>
<div id="show_table"></div>
<script src="table.js"></script>
</body>
</html>
table.js
const tableDiv=document.queryselector('#show_tabel')
result.forEach((obj)=>{ //<==== i want to pass that result data here.
tableDiv.innerHTML =result
})
P.S just want to know if this works then i can create table
CodePudding user response:
You might use a template engine like (ejs) or any other template engines to show the dynamic data that returns back from the server and for the case that you want to render the results in different page you should store the data in any kind of storages like local storage or database to be able to use it in different places.