Accessing MongoDB Data in a – Devin Patrick Ashcraft Apr 26 '19 at 14:29

  • I also tried the solution on this post with no success https://stackoverflow.com/questions/16098397/pass-variables-to-javascript-in-expressjs/16098699 – Devin Patrick Ashcraft Apr 26 '19 at 14:29
  • 2 Answers2

    0

    Try this. In your app.js file render results as JSON string like this.

    res.render('homePage',{results: JSON.stringify(results)});
    

    Now in your homepage.ejs use this:

    const locationResults = <%- results %>
    

    Note that it may cause some errors like "Expression expected .js" in your IDE. Just ignore it. Probably is VS Code compatibility with ejs files

    dimitris tseggenes
    • 3,031
    • 2
    • 16
    • 23
    • I replied to another answer that my problem is I cannot use <% %> tags inside my script tags at all. It returns a syntax error. Not sure of the cause but maybe something in my ejs is causing this problem so i added the full(but short) ejs to the main post – Devin Patrick Ashcraft Apr 26 '19 at 15:58
    • First, you forgot `=` in your JSON.stringify assignment. Second, use `forEach` on `locationResults` and not on `results`. – dimitris tseggenes Apr 26 '19 at 16:11
    • I appreciate the help,I had already tried that changed, but without sounding like a broken record even with these changes there is the syntax error, caused by the <%= %>. Everyone example I see uses these in the script tags with no flaw. – Devin Patrick Ashcraft Apr 26 '19 at 16:18
    • Same issue occurs. Do you think adding a > Outside of the script tags would work? Edit: it didn't work – Devin Patrick Ashcraft Apr 26 '19 at 16:45
    • try `const locationResults = JSON.parse('<%- results %>')` – dimitris tseggenes Apr 26 '19 at 16:54
    • So I saw your comment about the IDE causing the error. So I tried all the solutions so far and they compiled and ran. I used the forloop ``` <% for (const location of results) { %> addMarker({coords:{lat: <%= location.lat %>, lng: <%= location.lng %>}, title: <%= location.title %>}); <% } %> ``` and it compiles but did not add the markers to the map. (yes the database is populated) – Devin Patrick Ashcraft Apr 26 '19 at 17:14
    • Then the problem is inside the `addMarker` function. Make sure, that `props` are well defined. – dimitris tseggenes Apr 26 '19 at 22:23
    • addmarker function works properly if I hard code numbers and title. I tried ` addMarker({coords:{lat: <%= location.lat %>, lng:<%= location.lng %>}, title: <%= location.title %>}); ` I output the data and it's able to be printed to the console using <% console.log( location.lat) %> successfully and typeof to show the correct types – Devin Patrick Ashcraft Apr 26 '19 at 23:23
    0

    I would just write the variable out and use it in the function

    <script>
    const locationResults = <%= JSON.stringify(results, null, '\t\) %>;
    // Now you don't have to loop in the template, you can loop on the client
    locationResults.forEach(location => addMarker({ coords: {lat: location.lat, lng: location.lng}, title: location.title }));
    </script>
    
    djheru
    • 3,525
    • 2
    • 20
    • 20
    • Maybe I'm not clear on the problem at hand. Anytime I try and use <%= %> inside of the script tags I get a syntax error "JS expression expected". So either I need to figure out why it isn't accepting the tags or I need another way to use my data inside the script. All examples so <% %> working inside. I will add the full EJS file so clarity. – Devin Patrick Ashcraft Apr 26 '19 at 15:55