简体   繁体   中英

Mongoose findByIdAndUpdate is not updating data

Can't seem to see why findByIdAndUpdate is not updating the data. I changed it to findByIdAndRemove to see if I could spot the error but when I change it, it did delete so I couldn't track down the issue. Appreciate your help in advance.

Here is the route

    router.get("/:id/edit", function(req, res){
    Product.findById(req.params.id, function(err, foundProduct){
        res.render("admin/edit",{product: foundProduct});
    });
});


router.put("/:id",function(req, res){
   Product.findByIdAndUpdate(req.params.id, req.body.product, function(err, updatedProduct){
       if(err){
           res.redirect("/");
       }else{
           res.redirect("/admin/inventory");
       }
   });
});

Here is the button to get to edit form:

<h2>This is the inventory page!</h2>

<% include ../partials/header%>

<a href="/admin/new">Add a new product</a>

    <div class = "row">
    <% products.forEach(function(product){ %>
            <div class = "col-sm-6 col-md-4">
                <div class = "thumbnail">
                    <img src = <%=product.imagePath%> alt = "..." class = "img-responsive">
                        <div class = "caption">
                            <h3><%=product.title%> </h3>
                                <p class = "description"> <%=product.description%> </p>
                                <div class = "clearfix">
                                <div class = "price pull-left">$<%=product.price%>  </div><br><br>
                                <a href = "/admin/<%=product._id%>/edit"class = "btn btn-info pull-right" role = "button" > Edit</a>
                <form id="delete-form" action="/admin/<%= product._id %>?_method=DELETE" method="POST">
                           <button class="btn btn-danger">Delete</button>
                       </form>
                                </div>
                        </div>
                </div>
            </div>
  <% }); %>
    </div>



<% include ../partials/footer%>

Here is the edit form:

<% include ../partials/header %>

<h2>This is the inventory edit page</h2>


<div class="container">
    <div class="row">
    <h1 style="text-align: center">Edit</h1>
    <div style="width: 30%; margin:25px auto">
        <form action="/admin/<%=product._id%>?_method=PUT" method="POST">
            <div class="form-group">
                <input class="form-control" type="text" name="product[title]" value="<%=product.title%>">
            </div>
             <div class="form-group">
                <input class="form-control" type="text" name="product[imagePath]" value="<%=product.imagePath%>">
            </div>
             <div class="form-group">
                <input class="form-control" type="text" name="product[description]" value="<%=product.description%>">
            </div>
            <div class="form-group">
               <input class="form-control" type="text" name="product[price]" value="<%=product.price%>">
           </div>
            <div class="form-group">
                <button class="btn btn-lg btn-primary btn-block">Submit!</button>
            </div>
        </form>
         <a href="/">Go Back</a>
    </div>

    </div>
</div>

<% include ../partials/footer %>

Here is the product model:

var mongoose = require('mongoose');

module.exports = mongoose.model('Product',{
  imagePath: {type: String, required: true},
    title: {type: String, required: true},
    description: {type: String, required: true},
    price: {type: Number, required: true}
});

Maybe the problem is here:

router.put("/:id",function(req, res){
   Product.findByIdAndUpdate(req.params.id, req.body.product, function(err, updatedProduct){
       if(err){
           res.redirect("/");
       }else{
           res.redirect("/admin/inventory");
       }
   });
});

Change it to:

router.put("/:id",function(req, res){
   Product.findByIdAndUpdate(req.params.id, { imagePath: req.body.imagePath, ..... }, function(err, updatedProduct){
       if(err){
           res.redirect("/");
       }else{
           res.redirect("/admin/inventory");
       }
   });
});

Your form is using "extended format" parameter names:

<input class="form-control" type="text" name="product[title]" ...>

However, if you disable extended format parsing in body-parser , the resulting value in req.body will be wrong:

{ 'product[title]' : 'value' }

(instead of { product : { title : 'value' } } )

You should enable extended format parsing:

app.use(bodyParser.urlencoded({ extended : true }));

Documentation: https://github.com/expressjs/body-parser#extended

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM