code:这是我尝试的第一个方法,它给出
错误=比;转换到ObjectId的值失败"600 b0757ae00113644624036";目录
router.post("/addnewProduct",async (req,res)=>{
if(req.body._id){
const nImage = req.files.image.name
productSchema.findOneAndUpdate({_id:req.body._id}).then((oldp)=>{
oldp.title = req.body.title,
oldp.category = req.body.category,
oldp.description = req.body.productDescription,
oldp.price = req.body.price,
oldp.image = nImage
oldp.save().then((value) => {
console.log("updated")
})
})
error: error =>转换到ObjectId的值失败"600 b0757ae00113644624036";目录
第二种方法:
router.post("/addnewProduct",async (req,res)=>{
if(req.body._id){
productSchema.findOneAndUpdate({id:req.body._id},
{$set: {title:req.body.title,category:req.body.category,description:req.body.productDescription,price:req.body.price,image:req.files.image.name}},
{multi:true}
).then(()=>{
res.redirect("/adminAdd/editAddProduct")
})
将findOneAndUpdate中的id更改为_id,如下所示
router.post("/addnewProduct",async (req,res)=>{
if(req.body._id){
productSchema.findOneAndUpdate({_id:req.body._id},
{$set: {title:req.body.title,category:req.body.category,description:req.body.productDescription,price:req.body.price,image:req.files.image.name}},
{multi:true}
).then(()=>{
res.redirect("/adminAdd/editAddProduct")
})