Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

292
Views
Writing EJS in JS script dynamically

i am trying to use Leaflet API and store my users saved points(markers,circles,polygons) into a MongoDB database.

Is there a more elegant and dynamic way of writing a JS script in a HTML page while getting the results from the database?

at the moment i am getting my data from Mongo and passing it through the Get request and using that on the HTML page, writing EJS inside the JS script. it works fine but im looking into a better solution.

many thanks for your time

    // index.js
    router.get('/index', ensureAuthenticated, (req, res) => {
      var queryz = Points.find({ belongs_to: req.user._id })
      queryz.exec(function (err, results) {
        if (err) return handleError(err);

        res.render('index', {
          user: req.user,
          points: results,
        })
      })
    })
    // index.ejs
      // looping through the points from the database to dynamically add the points 
            // each time the map is called
            <% if(typeof points != "undefined") { %>  // making sure user is logged in
            <% counter = 1 %>
            <% if(points.length>0){ %>
                <% points.forEach(p => {%>
                <% if(p.type == "marker"){ %>  // if point is a marker
                    marker<%=counter%> = L.marker([<%=p.coords%>], { icon: <%=p.icon%>, alt: '<%=p.popup_message%>' }).bindPopup('<%=p.popup_message%><form method="post" action="/delete_point" id="pointFORM"><input id="pointID" name="pointID" value="<%=p._id%>"><button id="submitBtn" type="submit">🚫</button></form> ').openPopup()
                    <% counter++%>
                    <% } %>
                <% if(p.type == "circle"){ %>    // if point is a circle
                    marker<%=counter%> = L.circle([<%=p.coords%>], { color: '<%=p.color%>', fillColor: '<%=p.fill_color%>', fillOpacity: <%=p.fill_opacity%>, radius: <%=p.radius%> }).bindPopup('<%=p.popup_message%>').openPopup()
                    <% counter++%>
                    <% } %>
                <% if(p.type == "polygon"){ %>    // if point is a polygon
                    marker<%=counter%> = L.polygon([<%=p.coords%>], ).bindPopup('<%=p.popup_message%>').openPopup()
                    <% counter++%>
                    <% } %>
                <% });%>
            <% } %>
            
        <% } %>
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I'm assuming the problem space here is that you want to dynamically fetch some data from your server and insert that into a web page using Javascript in the web page.

Given that, you have several options:

  1. You can make an Ajax call to your server and have your server return fully formed HTML which your client-side Javascript can then insert directly into the page. That appears to be what you are illustrating already in your question.

  2. You can make an Ajax call to your server and have your server return JSON which your client-side Javascript can then turn into HTML itself and insert that into the page. You can either manually code the conversion into HTML in Javascript or you can use client-side rendering with EJS to convert the data into HTML using a client-side EJS template.

  3. You can reload the entire page and let the server render the whole new page, presumably including the latest data.

For incremental updates to a page, options 1 and 2 are generally preferable to option 3 and are usually more efficient for all.

The choice between 1 and 2 is really just an architectural preference. Some developers of high scale sites would rather offload as much processing to the client as possible and thus prefer letting the client do the rendering of the new HTML as in option 2, but that means you have to have both the EJS template and client-side EJS rendering in the web page which is more client-side weight. So, that's a tradeoff too.

There really aren't options that are simpler than these, so if what you have now (option 1) is working just fine you can stay with that.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!