'My object is being logged and set in the front end of React, but it is not being saved in the database
I'm trying to pass in my One Rep Max object which is a variable of my Plan object, through my Plan object.
function FiveThreeOne(props) {
const history = useHistory();
const [user, setUser] = useState({});
const [orm, setOrm] = useState({
benchPressMax: '',
squatMax: '',
overHeadPressMax: '',
deadliftMax: '',
})
useEffect(() => {
const params = {
id: localStorage.getItem("loggedInUser"),
}; console.log(localStorage)
axios.get(`http://localhost:8080/getUser/${params.id}`, {
headers: {
'Content-Type': 'application/json'
}
}).then((response) => {
setUser(response.data);
}).catch((error) => {
console.log('error in getting account')
});
}, []);
const ormChangeHandler = (event) => {
const name = event.target.name;
const value = event.target.value;
const tempOrm = { ...orm };
tempOrm[name] = value;
setOrm(tempOrm)
console.log(orm)
}
/*const ormSubmitHandler = () => {
user.plan.orm = Object.assign({orm})
axios.post(`http://localhost:8080/addORM`, orm, {
headers: {
'Content-Type': 'application/json'
}
}).then((response) => {
setOrm(response.data)
})
}*/
const [plan, setPlan] = useState({
name: 'Five Three One',
ormId: orm,
})
const planSubmitHandler = () => {
plan.ormId = Object.assign({orm})
console.log(plan)
axios.post(`http://localhost:8080/createPlan/${user.id}`, plan, {
headers: {
'Content-Type': 'application/json'
}
}).then((response) => {
setPlan(response.data)
history.push('/user-profile')
})
}
One of my change handlers:
<input type="number" name="benchPressMax" value={orm.benchPressMax} id="inputBenchPressMax" placeholder='Enter bench' onChange={ormChangeHandler}/>
<table className='workout-table' id='workout-list'>
<thead id='workout-list'>
My submit handler:
<div><button className='workoutList-button' onClick={planSubmitHandler}>Add</button></div>
The plan object is being passed and saved, but the ORM is being set to null in my back end. The conlog shows that the object is populated.
Front end console log:
{name: 'Five Three One', ormId: {…}}
name: "Five Three One"
ormId:
orm:
benchPressMax: "1"
deadliftMax: "1"
overHeadPressMax: "1"
squatMax: "1"
Back end:
public Plan createPlan(Long id, Plan plan) {
User account = userService.getAccount(id);
Plan newPlan = plan;
plan.setPlanStart(LocalDate.now());
account.getPlan().add(newPlan);
OneRepMax planORM = plan.getOrmId();
System.out.println(planORM.getBenchPressMax());
ormRepo.save(planORM);
planRepo.save(newPlan);
userRepo.save(account);
return newPlan;
}
The benchpressMax shows null in my eclipse console.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
