Here is the schema I am using for my model:
const workoutSchema = mongoose.Schema({
workouts: [
{
workoutName: String,
sets: Number,
reps: Number,
weight: Number,
},
],
});
Below is the postData referenced in the text field.
const [postData, setPostData] = useState({
workouts: [{ workoutName: "", sets: "", reps: "", weight: "" }],
});
In order to access the workoutName mentioned above, I have set the value to
postData.workouts["workoutName"]
, which seems to work but I'm not entirely certain. My issue lies with the onChange property, particularly with the second argument in setPostData [workoutName]: e.target.value
. I am struggling to access the workoutName
key within the workouts dictionary from the schema.
<TextField
label="Workout"
variant="outlined"
value={postData.workouts["workoutName"]}
onChange={(e) =>
setPostData({ ...postData, workoutName: e.target.value })
}
I have attempted
workouts["workoutName"]: e.target.value
, but it results in a syntax error before the bracket, indicating that a comma was expected. See the error image. Additionally, I tried workouts.workoutName : e.target.value
, as in other programming languages, but it did not work. How can I properly access the workoutName within the workouts object to update it with the value typed in the text field (i.e., e.target.value
)?