您好,我在运行代码时遇到问题,因为它说E11000重复键错误收集
router.put('/:city/edit',isAdminCheck, async function(req,res){
var query = req.params.city;
var update = {region:req.body.region,country:req.body.country,image:req.body.image,flag:req.body.flag,languages:req.body.languages,currencies:req.body.currencies,city:req.body.city};
CityList.findOneAndUpdate(query,update,{upsert:true}, function(error){
if(error){
if(error.code === 11000){
CityList.findOneAndUpdate(query,update,{upsert:true}, function(error){
if(error){
console.log(error);
}else{
res.redirect("/" + req.params.cty);
}
})
}
}else{
res.redirect("/" + req.params.city);
}
})
});
答案 0 :(得分:0)
您的查询不正确。应该是这个
var query = {city: req.params.city};
并且您进行两次相同的更新。您只能调用一次。
CityList.findOneAndUpdate(query,update,{upsert:true}, function(error){
if(error){
// handle error
}
}else{
res.redirect("/" + req.params.city);
}
})