Here is my useSend function, I am working with Typescript + React.
const useSend = (method: string, url: string, data: any): Promise<string> => {
return fetch(url, {
method: method,
headers: {
"Content-Type": "application/json",
"Accept": "application/json", },
credentials: 'include',
body: JSON.stringify(data),
})
.then((response) => {
if (response.status === 200) {
return "success";
} else {
response.json()
.then((resp) => {
return resp;
});
}
})
.catch(err => {
console.log(err)
return err;
});
}
export default useSend;
When I call .then on useSend, an error is always thrown :
Cannot read properties of undefined (reading ‘then’)
TypeError: Cannot read properties of undefined (reading ‘then’)
function useSubmit (event: React.FormEvent<HTMLFormElement>) {
event.preventDefault();
const form = new FormData(event.currentTarget);
const data = { email: form.get('email'), password: form.get('password') };
useSend("POST", "http://localhost:2999/login", data)
.then((temp: string) => {
console.log(temp);
result = temp;
if (result == "success") {
window.location.href = "/feed/latest";
}
})
}
I have made changes to the return statements and block structures but nothing helps, can someone help to explain why void is returned by this function even tho I have used then at every step to get the outer function to return after the promise is resolved?