我有一个组件,它是有条件地设置值的基础上一块状态和按钮点击,通过数据对象的RTK查询和发送到一个控制器。有两个方面是令人困惑的我。第一,控制台日志显示,值得到设置成状态是相反的,他们应该是什么,和第二,mongoose/express控制器不会根据req.body中传递的值进行响应,而是继续提供已经存在的值。
该按钮是用户切换提交或取消提交的方式,如果提交已经为真,则按钮正确显示为取消提交,onClick应将值设置为false和2个空字符串,但提供相反的结果。如果提交为假,则按钮正确显示为提交,onClick应将值设置为true并填充字符串,但提供相反的结果。
React组件:
const Submit = ({ planId, plan, user, today}) => {
const [isSubmitted, setIsSubmitted] = useState(plan.submitted);
const [submitBy, setSubmitBy] = useState(plan.submittedBy);
const [submitDate, setSubmitDate] = useState(plan.submittedDate);
const [skip, setSkip] = useState(true)
const [userId, setUserId] = useState()
const [updateStatus] = useUpdateReportStatusMutation()
const {data} = useGetUserQuery(userId, {skip})
useEffect(() => {
if(isSubmitted) {
setUserId(plan.submittedBy),
setSkip(false)
}
}, [isSubmitted])
const onSubmit= async (data) => {
console.log('onSubmit: ', data) //This shows the data as reflected by the onClick functions
try {
const payload = await updateStatus({ planId, data }).unwrap();
console.log('fulfilled', payload) //continues to show the same already existing data
} catch (err) {
console.error('Failed to update report status: ', err)
}
toast.success("Report Status Updated", {
position: toast.POSITION.TOP_RIGHT
});
}
const handleUnsubmitValues = () => {
setIsSubmitted(false)
setSubmitBy('')
setSubmitDate( '')
console.log("values for unsubmit: ", isSubmitted, submitBy, submitDate) //this is currently logging- isSubmitted: true, submitBy: 'the users ID', submitDate: 'an ISO date string'
handleSubmit()
}
const handleSubmitValues = () => {
setIsSubmitted(true)
setSubmitBy(user)
setSubmitDate(today)
console.log("values for submit", isSubmitted, submitBy, submitDate) //this is logging- isSubmitted: false, submitBy: '', submitDate: ''
handleSubmit()
}
const handleSubmit = () => {
const data = {
submitted: isSubmitted,
submittedBy: submitBy,
submittedDate: submitDate
}
console.log("before submit", data) //this logs the same values as the function call
onSubmit(data)
}
const subDate = new Date(plan.submittedDate).toLocaleDateString()
return (
<>
{isSubmitted ? (
<>
<CheckBoxIcon />
<label>{data?.fullName}</label><br />
<label>{subDate}</label><br />
<Button color='warning' variant='contained' onClick={handleUnsubmitValues}>Unsubmit</Button>
</>
) :
<>
<CheckBoxOutlineBlankIcon />
<Button color='primary' variant='contained' onClick={handleSubmitValues}>Submit Report</Button>
</>
}
<br />
</>
)
}
字符串
mongoose/ express控制器:
exports.updateReportStatus = async (req, res) => {
console.log("req.body", req.body) //logs the values as supplied by the onSubmit
try {
const servicePlan = await ServicePlan.findOne({ _id: req.params.planId })
if (req.body.submitted) {
servicePlan.submitted = req.body.submitted
}
if (req.body.submittedBy) {
servicePlan.submittedBy = req.body.submittedBy;
}
if (req.body.submittedDate) {
servicePlan.submittedDate = req.body.submittedDate
}
if (req.body.approved) {
servicePlan.approved = req.body.approved
}
if (req.body.approvedBy) {
servicePlan.approvedBy = req.body.approvedBy;
}
if (req.body.approvedDate) {
servicePlan.approvedDate = req.body.approvedDate
}
if (req.body.sent) {
servicePlan.sent = req.body.sent
}
if (req.body.sentBy) {
servicePlan.sentBy = req.body.sentBy;
}
if (req.body.sentTo) {
servicePlan.sentTo = req.body.sentTo
}
if (req.body.sentDate) {
servicePlan.sentDate = req.body.sentDate
}
console.log("status ", servicePlan) //logs the same existing values, regardless of req.body
servicePlan.save()
res.send(servicePlan)
} catch (error) {
res.status(404)
res.send({ error: "Error updating report status", error})
}
}
型
用于提交的值的控制台日志(应该为true和填充字符串):
values for submit false <empty string> <empty string>
before submit
Object { submitted: false, submittedBy: "", submittedDate: "" }
onSubmit:
Object { submitted: false, submittedBy: "", submittedDate: "" }
型
unsubmit的console.log值(应该是false和空字符串,如上所述)
values for unsubmit: true 62410a1dcaac9a3d0528de7a 2022-09-15T15:55:27.758Z
before submit
Object { submitted: true, submittedBy: "62410a1dcaac9a3d0528de7a", submittedDate: "2022-09-15T15:55:27.758Z" }
onSubmit:
Object { submitted: true, submittedBy: "62410a1dcaac9a3d0528de7a", submittedDate: "2022-09-15T15:55:27.758Z" }
型
下面是一个网络调用,显示了在请求中传递的主体和没有响应的响应:
x1c 0d1x的数据
的
对于记录和传递值的状态问题,我尝试切换onClick调用的函数,但这只是使控制按钮的状态没有更新。我唯一的猜测是,在设置状态和将值传递给前端的handlesubmit之间,函数调用有延迟,只要后端没有用req.body值更新模型,我完全不知道该怎么办。
1条答案
按热度按时间vxbzzdmp1#
我通过引入React-Hook-Form并在更新函数中的CheckBox Icons和setValue上使用register来使前端正常工作。
它没有解释为什么状态没有正确地更新到函数中提供的值,所以我在那里不知所措。
至少现在,使其成为onSubmit调用的值适合于传递的内容。
字符串