Mongoose findByIdAndUpdate不更新数据

时间:2017-07-23 06:37:52

标签: javascript node.js mongodb express mongoose

似乎无法理解为什么findByIdAndUpdate没有更新数据。我将它更改为findByIdAndRemove以查看是否可以发现错误,但是当我更改它时,它确实删除了所以我无法追踪问题。提前感谢您的帮助。

这是路线

    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");
       }
   });
});

以下是编辑表单的按钮:

<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%>

以下是编辑表格:

<% 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 %>

以下是产品型号:

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}
});

2 个答案:

答案 0 :(得分:1)

问题就在这里:

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");
       }
   });
});

将其更改为:

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");
       }
   });
});

答案 1 :(得分:1)

您的表单正在使用&#34;扩展格式&#34;参数名称:

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

但是,如果您在body-parser中禁用扩展格式解析,req.body中的结果值将会出错:

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

(而不是{ product : { title : 'value' } }

您应该启用扩展格式解析:

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

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