Home > OS >  deleteOne mongoose problem express and node
deleteOne mongoose problem express and node

Time:11-13

I have this code in my main.app:

app.get('/admin_panel', isAdm, async (req, res)=>{
  var records = await add_joke.find({});
  res.render('admin_panel', { recs: records });
})

After this I have and ejs file called admin_panel with this code:

<% for(var i in recs){ %>
          <div >
            <p ><%= recs[i].title %></p>
            <p ><%= recs[i].description %></p>

            <a href="/profiles?username=<%= recs[i].post_user %>" >By 
            <%=recs[i].post_user %></a>

            <form action="/admin_panel" method="post">
              <input type="hidden" name="id_post" value="<%= recs[i].id %>">
              <input type="submit" value="Delete">
            </form>

          </div>
<% } %>

And that submit to delete is:

app.post('/admin_panel', async (req, res)=>{
   var the_records = req.body;
   console.log(the_records);
   var the_alg_find = await add_joke.deleteOne({id: the_records.id_post});
   console.log(the_alg_find);
   res.redirect('/admin_panel');
})

The problem is that that post form gives me the rest of the id of the first post in the database to all post, it's there another method to select a specific item from an iteretion like this example? Thank you!

CodePudding user response:

To better understand this you should probably share your collection schema

CodePudding user response:

In the screenshot that you posted, the documents in the database do not have an id field. Therefore, you cannot refer to recs[i].id, and queries on { id: somevalue } will not work. Did you mean to use the _id field, instead?

  • Related