English 中文(简体)
为什么这一职能恢复无效?
原标题:Why does this function return void?

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";
        }
    })
}

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

问题回答

首先,你点名的是<代码>不适用(<>>>>/代码>,不是重读。 更不用说把这一职能重新命名为其他职能。

其次,在<代码>response.json(......)前,你没有<代码>return,因此,你没有再回升价值。

第三,如果你提供全方位的痕迹,而不仅仅是错误的信息,将是有益的。 全部脚印应当表明该法典的错误来自该法典。

[Edit] 第四,正如@Pac0在其评论中建议的那样,请提供实际上称作useSend(<>”的代码,因为该代码正从该错误中删除。





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签