简体   繁体   English

我的程序正在“跳过”一个获取请求,React.js

[英]My program is 'skipping' a fetch request, React.js

Its really weird.它真的很奇怪。

The first fetch I do works appropriately, but when doing the second fetch in my handleSubmit() method it sort of 'skips' it.我做的第一次提取工作正常,但是在我的 handleSubmit() 方法中进行第二次提取时,它有点“跳过”它。 It goes on, never enters the .then statements, doesn't print an error.它继续,从不进入 .then 语句,不打印错误。 Ive tried with other APIs, but honestly it should work fine since the first take is almost identical and it worked.我已经尝试过其他 API,但老实说它应该可以正常工作,因为第一次几乎相同并且可以正常工作。 Ive tried rewriting with a return statement as well...我也试过用 return 语句重写......

export default function FormContainer() {
    const [api, setApi] = useState()
    const [showText, setShowText] = useState(false)
    const [apiUrl, setApiUrl] = useState('')
    const [text, setText] = useState('')
    const [display, setDisplay] = useState('')
    const [page, setPage] = useState('')

    useEffect(() => {
        fetch('https://swapi.co/api/') //FIRST TRY, works
        .then(response => response.json())
        .then(response => setApi(response))

    },[])

    function handleRadio(event){
        setShowText(true)
        setPage(event.target.id)
        setApiUrl(api[event.target.id])
    }

    function handleText(event){
        setText(event.target.value)
    }

    function handleSubmit(event){
        event.preventDefault();
        let list = {};
        let found = false;

        console.log(apiUrl); //Prints
        fetch(apiUrl) //SECOND TRY, fails
        .then(response =>{
            console.log(response); //Never prints
            return response.json();
        })
        .then(response => {
            console.log(response);
        })
        .catch(error => {
            console.error(error); //Doesnt run
        })


        while(!found){
            list.results.map(item => {
                if(item.name.toUpperCase() === text.toUpperCase()){
                    found = true
                    let toDisplay = ''
                    if(page === 'people'){
                        console.log(page)
                    }else if(page === 'planets'){
                        console.log(text)
                    }else if(page === 'films'){
                        console.log(page)
                    }else if(page === 'species'){
                        console.log(page)
                    }else if(page === 'vehicles'){
                        console.log(page)
                    }else{
                        console.log(page)
                        //Starships
                    }
                }
            })
            if(!found){
                if(list.next !== null){
                    fetch(list.next) //DIDNT GET TO TRY THIS
                    .then(response => response.json())
                    .then(response => {list = response})
                }else{
                    found = true
                    setDisplay('Object not found, are you sure you typed it in correctly?')
                }

            }
        }
    }

  return (
    <div >
      <FormRadios handleRadio={handleRadio}/>
      <br></br>
      {showText ? <FormComponent text={text} handleText={handleText} handleSubmit={handleSubmit}/> : null}
      <hr></hr>
      <FormOutput display={display}/>
    </div>
  );
}

Of course, I welcome any kind of advice on my code, since Im brand new on React.js and using hooks.当然,我欢迎对我的代码提出任何建议,因为我在 React.js 和使用钩子方面是全新的。 Thanks in advance!提前致谢!

It appears successful, but just now I realized what my problem was.看起来成功了,但我刚刚意识到我的问题是什么。 When fetching again for the api, i jump to the next line of code immediately, but the fetch isnt as instant as the code running.当再次获取 api 时,我立即跳转到下一行代码,但获取并不像代码运行那样即时。 I had an exception going on after the fetch (because im trying to use info from the fetch), so the fetch couldnt finish fast enough to use the information correctly, then the exception runs and, of course, the console logs dont work.我在获取后发生了异常(因为我试图使用来自获取的信息),因此获取无法以足够快的速度完成以正确使用信息,然后异常运行,当然,控制台日志不起作用。 Thats basically what happened, and quite interesting as well.这基本上就是发生的事情,而且也很有趣。 But thanks for all the help, it really helped me fin what was going on.但是感谢所有的帮助,它真的帮助我弄清楚发生了什么。

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

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