title: Why are likes not being updated on button click in my mongoose schema?
body: <p>I am working on an express app using nodejs and mongoose, i am trying to use js function to update the number of likes on a post in database in my ejs file but it is not working , i have tried onclick() also, bascically anything inside the script tag is not running . Why is this happening , and how can i update the likes of a particular post in database by clicking a button.</p>
  <p>Though, i have achieved this likes updation by redirecting to a route in app.js where mongoose function updateOne does it , but i want likes updated without reloadig the page on a button click</p>
  <p>update likes route in app.js</p>
  <pre><code>app.get('/liked/:id', (req,res)=&gt;{
      Post.updateOne({_id: req.params.id}, {$inc: {likes:1}}, (err,likedpost)=&gt;{
          if(err) console.log(err)
          else{
              res.redirect(&quot;/posts/engineering&quot;);
          }
     })
  })
  </code></pre>
  <p>show.ejs</p>
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html lang=&quot;en&quot;&gt;
  &lt;head&gt;
    &lt;meta charset=&quot;UTF-8&quot;&gt;
    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
    &lt;title&gt;Document&lt;/title&gt;
  &lt;/head&gt;
  
  &lt;body&gt;
    // here i want to add post model of mongoose
       var Post = require(&quot;../../models/post.js&quot;)
  
  
    &lt;h1&gt; &lt;%= post.title %&gt; &lt;/h1&gt;
    &lt;h3&gt; &lt;%= post.publish_date %&gt; &lt;/h3&gt;
    &lt;p&gt; &lt;%= post.content %&gt;&lt;/p&gt;
    &lt;button class=&quot;likes&quot;&gt;like&lt;/button&gt;  
  
  &lt;/body&gt;
  &lt;script&gt;
    console.log(&quot;like button increment&quot;);              // not printing on console
    var l = document.querySelector(&quot;.likes&quot;);
    l.addEventListener(&quot;click&quot;, function(){
      console.log(&quot;***********&quot;);
      // here i wanted to update the &quot;likes&quot; field of this post by 1 i.e when user clicks the like button without reloading the page
  
       Post.updateOne({_id: post._id}, {$inc: {likes: 1}});
      console.log(post.likes + &quot;people liked&quot; +  post.title)
  
    })
  &lt;/script&gt;
  
  
  &lt;/html&gt;
  </code></pre>