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";
}
})
}
我对返回声明和封锁结构做了改动,但没有任何帮助,我能否帮助解释为什么这一职务被退回,哪怕是我曾经在每一步骤中利用过,以便在承诺得到解决后恢复外部职能?