I have this post method to create a new record in my database:
fetch('https://localhost:44326/api/Players/post', {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
handle: data.handle,
role: data.role,
avatar: linkToAvatar,
}),
})
.then((response) =>
console.log(response.json().then((data) => console.log(data.id))),
)
.then(() => console.log(newID))
.then(() => navigate('../Stats/', { state: { userID: newID } }));
};
This part of that code
.then((response) =>
console.log(response.json().then((data) => console.log(data.id)))
Gets the response back and logs the id of the newly created record and it works fine.
Above this code, I create a const newID
const [newID, setNewID] = useState();
In the fetch block if I replace the console.log(data.id)
with setNewID(data.id),
the value of newID is never set to anything. It remains undefined. Why is this? If the newly created record's id is 80, then newID should be set to 80 there, but it isn't.
This is most of that page in case you need it:
import React, { useState } from 'react';
import { useForm } from 'react-hook-form';
import styles from './styles.module.css';
import { useNavigate } from 'react-router';
import S3 from 'react-aws-s3-typescript';
import { config } from '../../config';
import { prototype } from 'events';
type FormData = {
handle: string;
role: string;
avatar: FileList;
};
const ReactS3Client = new S3(config);
function PlayerCreatePage() {
const navigate = useNavigate();
const [newID, setNewID] = useState();
const {
register,
handleSubmit,
formState: { errors },
} = useForm<FormData>();
const onSubmit = (data: FormData) => {
var avatarName = null;
var postedName = null;
if (data.avatar[0]?.name == null) {
avatarName = null;
postedName = null;
} else {
avatarName = data.avatar[0]?.name;
postedName = data.avatar[0]?.name;
}
var imgType = data.avatar[0]?.name.substr(data.avatar[0]?.name.length - 3);
if (imgType === 'jpg') {
postedName = avatarName?.substring(0, avatarName.length - 4);
avatarName = avatarName?.replace('jpg', 'jpeg');
} else {
postedName = postedName?.substring(0, postedName.length - 4);
}
var linkToAvatar = null;
if (avatarName == null) {
linkToAvatar = null;
} else {
linkToAvatar =
'https://cyberpunkv2.s3.us-east-2.amazonaws.com/avatars/' + avatarName;
}
if (postedName != null) {
ReactS3Client.uploadFile(data.avatar[0], postedName)
.then((data) => console.log(data))
.catch((err) => console.error(err));
}
fetch('https://localhost:44326/api/Players/post', {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
handle: data.handle,
role: data.role,
avatar: linkToAvatar,
}),
})
.then((response) =>
console.log(response.json().then((data) => setNewID(data.id))),
)
.then(() => console.log(newID))
.then(() => navigate('../Stats/', { state: { userID: newID } }));
};
On the second from bottom line where I log newID
it is always 'undefined'. What am I doing wrong or how have I misunderstood the process?
I don't think you need to actually see the render method but if for some reason you do just ask.
EDIT:
Could I not simply do this and forget about the state of newID altogether?
.then((response) =>
console.log(
response
.json()
.then((data) =>
navigate('../Stats/', { state: { userID: data.id } }),
),
),
);