I am using axios to send information from my React app to my express API, and when I print the typeof a value before I send the information, it is a boolean (which I do have it typed as) but when I receive it in my API and print the typeof, it is converted to and stored as a string.
the type as it it typed in both the API and the React app:
export interface questionAsked {
text: string;
answerGiven: answers[];
}
export interface answers {
isCorrect: boolean;
text: string;
}
How I send the information from my React app:
const [APIResonse, setAPIResponse] = useState<number>(0);
const sendAnswersAndTime = async (): Promise<void> => {
try {
const response = await axios.post("http://localhost:3000/updateAnswers", {
params: {
username: "1234",
questionAsked: questionsAsked,
},
});
const { data } = response;
setAPIResponse(data);
} catch (error) {
console.log(error);
}
};
useEffect(() => {
sendAnswersAndTime();
}, []);
How I receive the information in the API
I now have
app.use(express.json());
app.use(cors());
app.post("/updateAnswers", async (req, res) => {
const topicName = req.body.topicName;
const clientNumber = req.body.username;
const questionAsked = req.body.questionAsked;
console.log(questionAsked, " hello ");
to which it is now undefined. Why would this be returning undefined?
What I have tried is using JSON.stringify and then JSON.parse, this did not work and I also attempted to simply change this axios request from GET to POST and this caused the app to crash.