问题描述
错误 ** 无法修补 /updatepost/60e***************8a
在这个项目中,我无法更新我的 mern 堆栈项目中的数据,请在我的项目中帮助我,我提供了我的项目的一些代码,我只想使用数据库(Mongodb)更新我的项目数据>
const express= require("express");
const mongoose=require("mongoose");
const router03=express.Router();
const Sch=require("../model/blogSchema");
router03.patch("/updating/:id",async (req,res)=>{
const items={uname,utname,umyblog}=req.body;
const id01=req.params.id;
try{
const updata = await Sch.findByIdAndUpdate({id:id01},{$set:items});
res.send(updata);
}catch(err){
res.status(404).send(err);
}
});
module.exports=router03;
项目中的模型
const mongoose=require('mongoose');
const blogSchema=mongoose.Schema({
tname:{
type:String,require:true,},myblog:{
type:String,name:{
type:String,}
});
const UserInfo=mongoose.model('BLOGINFO',blogSchema);
module.exports=UserInfo;
import React,{ useState } from "react";
import DeleteIcon from '@material-ui/icons/Delete';
import Button from '@material-ui/core/Button';
import UpdateIcon from '@material-ui/icons/Update';
import axios from 'axios';
const Form=(props)=>{
const [upost,setUpost]=useState({
uname:'',utname:'',umyblog:'',});
const [openit,setopenit]=useState(false);
const deleteBtn= async(id01)=>{
try{
const res=await axios.delete(`http://localhost:3001/deletepost/${id01}`);
const data=await res.send("done !!!!");
console.log(data);
}catch(err){
console.log(err);
}
};
const updateIt=()=>{
setopenit(!openit);
}
const handlingChange=(e)=>{
const {name,value}=e.target;
setUpost((items)=>{
return{
...items,[name]:value,};
});
}
const editing=async ()=>{
const {uname,umyblog}=upost;
try {
await axios.patch(`http://localhost:3001/updatepost/${props.id}`,{uname,umyblog});
setopenit(!openit);
} catch (error) {
console.log(error);
}
}
return(
<>
<form method="GET">
<div className="outercard01" >
<h2 type="text" className="showname">{props.name}</h2>
<h2 type="text" className="showtname">{props.tname}</h2>
<p type="text" className="showblog">{props.myblog}</p>
<div className="btns"><Button className="delbtn" method="POST" onClick={()=>{deleteBtn(props.id)}} ><DeleteIcon/></Button>
<Button className="updatebtn" method="POST" onClick={updateIt}><UpdateIcon/></Button></div>
</div>
</form>
{openit?
<>
<form className="frms" method="PATCH">
<input type="text" name="name" onChange={handlingChange} placeholder="Enter FirstName" className="name" value={upost.name}/>
<input type="text" name="tname" onChange={handlingChange} placeholder="Topic Name" className="tname" value={upost.tname}/>
<textarea type="text" name="myblog" onChange={handlingChange} placeholder="enter your blog" rows="" column="" className="bloggs" value={upost.myblog}/>
<div className="btndiv">
<button className="submitbtn" method="PATCH" onClick={editing}>Edit</button>
</div>
</form>
</>
:null}
</>
);
}
export default Form;
解决方法
使用补丁更新mongodb中的mongoose datq