我在客户端有一个表单,用户可以更新有关成分的一些信息,其中的成分是否有库存(在这种情况下选中复选框)或缺货(在这种情况下相同的复选框)如前所述是未经检查的。)
以下表格:
<form action="/inventory/<%=ingredient._id%>?_method=PUT" method="POST" class="form">
<!-- <form class="formNew form" action="/inventory/<%=ingredient._id%>" method="POST" > -->
<div class="form-group">
<label>Ingredient's picture:</label>
<input type="text" name="item[photoName]" value="<%=ingredient.photoName%>" class="form-control">
</div>
<div class="form-group">
<label>Name:</label>
<input type="text" name="item[name]" value="<%=ingredient.name%>" class="form-control">
</div>
<div class="form-group">
<label>Number of items:</label>
<input type="number" name="item[number]" value="<%=ingredient.number%>" class="form-control" min="0">
</div>
<div class="form-group">
<div class="text-center"><label>Or check ingredient item:</label></div>
<%if(ingredient.exist){%>
<input class="form-control" type="checkbox" name="item[checkbox]" checked>
<% } else { %>
<input class="form-control" type="checkbox" name="item[checkbox]" unchecked>
<% } %>
</div>
<button class="text-center btn btn-success pull-right" type="submit">Save changes</button>
</form>
这是处理更新的我的发布路径(Node Js):
//更新路线
app.put("/inventory/:id", function(req, res){
function(err, updatedIngredient){
var ingredientUpdated={
photoName:req.body.item["photoName"],
name:req.body.item["name"],
number:req.body.item["number"],
exist:req.body.item["checkbox"].checked
}
Ingredient.findByIdAndUpdate(req.params.id, ingredientUpdated, {new: true}, function(err, updatedIngredient){
if(err){
console.log(err);
} else {
console.log(updatedIngredient);
if(req.xhr){
res.json(updatedIngredient);
} else {
req.flash("success", "You successfully updated the Ingredient's info");
res.redirect("/inventory/" + req.params.id);
}
}
})
})
最后,这是我的Ingredient的模式模型(mongoDb / mongoose):
var IngredientSchema = new mongoose.Schema({
name:String,
number:Number,
exist:Boolean,
author:{
id:{
type:mongoose.Schema.Types.ObjectId,
ref:'User'
},
username:String
},
photoName:String
})
然而,我的实施并没有奏效,我得到了错误:&#34;无法阅读财产&#39;已检查&#39;未定义&#34;
有谁知道如何解决这个问题?