【发布时间】:2021-11-21 17:41:54
【问题描述】:
我正在使用后端处理我的项目。
目前正在开发一个组件,该组件执行put 请求以更新customer bean 的详细信息。但是,我遇到了一种奇怪的行为。
组件:
function UpdateCustomer(props): JSX.Element {
const history = useHistory();
const [skipCount, setSkipCount] = useState(true);
const [firstName, setFirstName] = useState('');
const [lastName, setLastName] = useState('');
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
async function formData() {
try {
const response = await axios.put<CustomerModel>(globals.adminUrls.updateCustomer + props.location.state.id, {
firstName: firstName,
LastName: lastName,
email: email,
password: password
});
const updated = response.data;
store.dispatch(customerUpdatedAction(updated));
notify.success(CUSTOMER_SccMsg.CUSTOMER_UPDATED)
}
catch (err) {
notify.error(err);
}
}
const handleSubmit = (e) => {
e.preventDefault();
formData();
}
useEffect(() => {
if (skipCount) setSkipCount(false);
if (!skipCount) formData();
}, []);
return (
<div className="custom-field">
<h2>Update Customer</h2>
<div>
<form onSubmit={handleSubmit} >
<label>First name</label>
<input type="text" name="firstName" onChange={(e) => { setFirstName(e.target.value) }} />
<label>Last name</label>
<input type="text" name="lastName" onChange={(e) => { setLastName(e.target.value) }} />
<label>Email</label>
<input type="text" name="email" onChange={(e) => { setEmail(e.target.value) }} />
<label>Password</label>
<input type="text" name="password" onChange={(e) => { setPassword(e.target.value) }} />
<input type="submit" name="submit" />
</form>
</div>
</div>
);
}
export default UpdateCustomer;
后端服务:
public void updateCustomer(@Valid int id, CustomerDto customerDto) throws DoesNotExistException {
Customer customerDao = customerMapper.toDao(customerDto);
if (!custRepo.existsById(id))
throw new DoesNotExistException("Customer does not exist");
custRepo.saveAndFlush(customerDao);
后端控制器:
@PutMapping(value = "update/customer/{id}")
@ResponseStatus(HttpStatus.ACCEPTED)
@Override
public void updateCustomer(@PathVariable int id, @RequestBody CustomerDto customerDto) throws DoesNotExistException {
adminService.updateCustomer(id, customerDto);
}
我得到的结果:
-
出于某种原因,我能够将除
lastName之外的所有字段保存到一个常量中。它的行为与其他字段完全相同。但是,此特定字段将null发送到服务器,而其他字段则发送输入值。 -
而不是执行
update来 bean 它只是addsthis 作为一个单独的 bean。
为什么这很奇怪?
我有一个用于更新公司的相同组件,它似乎工作得很好。为什么这个组件的行为不同?
希望有人能解决这个问题。
谢谢。
【问题讨论】: