简体   繁体   English

为什么我的功能没有达到目标?

[英]Why isn't my function reaching this point?

I've been stuck for an hour trying to figure this out. 我被困了一个小时试图解决这个问题。 I have a function that now looks like 我有一个现在看起来像的功能

myfunction()
{
    console.log("myfunction called");//TEST
    Axios.post('someurl/' + id)
        .then(response =>
        {
            console.log("We're in the .then ...");//TEST
            onGoodRequest(response.data, response);
        })
        .catch(response =>
        {
            console.log("We're in the .catch ...");//TEST
            this.setState({ curUploadState: uploadStates.SHOWSTOPPER });
        });
    console.log("We're here now ...");//TEST
}

which I call inside my React component and for some reason it's getting stuck. 我在我的React组件中调用它,由于某种原因它被卡住了。 There are no JavaScript errors in the console and the only thing I see is 控制台中没有JavaScript错误,我唯一看到的是

myfunction called

So what could possibly be going on? 那么可能发生了什么? I even did 我什至做到了

    var x = Axios.post('someurl/' + id)
                .then(response =>
                {
                    console.log("We're in the .then ...");
                    onGoodRequest(response.data, response);
                })
                .catch(response =>
                {
                    console.log("We're in the .catch ...");
                    this.setState({ curUploadState: uploadStates.SHOWSTOPPER });
                });
    console.log(x);
    console.log("We're here now ...");

and nothing logged. 没有任何记录。

It looks like you are returning nothing because your arrow function is on the line above the curly braces. 您似乎没有返回任何内容,因为箭头功能位于花括号上方的行中。 Move the curly braces up a line: 将花括号向上移动一行:

myfunction()
{
    console.log("myfunction called");//TEST
    Axios.post('someurl/' + id)
        .then(response => {
            console.log("We're in the .then ...");//TEST
            onGoodRequest(response.data, response);
        })
        .catch(response => {
            console.log("We're in the .catch ...");//TEST
            this.setState({ curUploadState: uploadStates.SHOWSTOPPER });
        });
    console.log("We're here now ...");//TEST
}

=> is basically an implicit return. =>基本上是隐式返回。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM