eugeneoei
eugeneoei

Reputation: 210

map marker in script tags within ejs file not showing

I want to have two markers, one of the user's current location and the other will be the location of post in my app.
The marker of the user's current location is plotted on the map but not the marker belonging to the location of a post.
Post contains information of a place of interest with latitude and longitude pair.

I rendered an ejs view with post like this:

router.get('/posts/:id', function(req,res) {
  db.post.findOne({
    where: { id: req.params.id}
  }).then(function(post) {
    res.render('posts/post_detail', {post: post})
  });
});

post contains latitude and longitude information that I want to access to plot a marker on my leaflet map. I place script tags at the bottom of the ejs view like this:

<script>
  var post = <%- JSON.stringify(post) %>;
  console.log(post.latitude);
  console.log(post.longitude);
  L.marker([post.latitude, post.longitude]).addTo(map);
  console.log(map)
</script>

Both latitude and longitude are printed out on the console with the same values as in the database. I used the same L.marker code elsewhere to place markers on my map and it works perfectly fine but not in this case. I am getting --

Uncaught TypeError: t.addLayer is not a function error <--

Is there another way around where I can access the latitude and longitude values to plot a marker on the map?

Upvotes: 0

Views: 453

Answers (1)

Owen J Lamb
Owen J Lamb

Reputation: 254

The map is not defined in the scope of that script tag.

Upvotes: 0

Related Questions