1

I am currently fetching data from one endpoint. I want to store another api endpoints data in a state variable in the onload function. However I am not to sure how to fetch multiple end points.

the second end point which I need to call:

'/api/get_all_test_types'

 

This shows the code used to fetch one end point

useEffect(() => {
        async function onLoadCreateUnitTests() {
          
          const results = await get('get_tables_autocomplete/b', user.user)
    
          autoComplete.setTablesAutoComplete(results)
    
        }
    
        onLoadCreateUnitTests()
    
      }, [])

MarkCBall Code:

 useEffect(() => {
    async function onLoadCreateUnitTests() {

      const results = await get('get_tables_autocomplete/b', user.user)
      const resultsAllTestTypes = await get('/api/get_all_test_types')
      autoComplete.setTablesAutoComplete(results)
      setAllTestTypes(resultsAllTestTypes)
  

    }

    onLoadCreateUnitTests()

  }, [])

Error enter image description here

2 Answers 2

1
const results = await get('get_tables_autocomplete/b', user.user)
const resultsAllTestTypes = await get('/api/get_all_test_types')
autoComplete.setTablesAutoComplete(results)
setAllTestTypes(resultsAllTestTypes)

Alternatively, you could run these two queries at the same time like so. This will complete as soon as the slower of the two queries completes.

const getPromises = [
    await get('get_tables_autocomplete/b', user.user),
    await get('/api/get_all_test_types')
]
const getResponses = Promise.all(getPromises)
autoComplete.setTablesAutoComplete(getResponses[0])
setAllTestTypes(getResponses[1])

You could get more sophisticated with callbacks to that you start populating your as soon as the first get comes back like so:

get('get_tables_autocomplete/b', user.user).then(autoComplete.setTablesAutoComplete)
get('/api/get_all_test_types').then(setAllTestTypes)
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the indepth! I am using the first section of your code, however I am getting a setAllTestTypes is not defined error? The code I have used is updated in my answer, maybe I have written it in correctly?
Oh ignore, me, I need state to store the test type data!
Understood! You need to set up some some state for the value to live. I assume you are comfortable with react hooks. Try a line like const [allTestTypes, setAllTestTypes] = useState() to create a place for that data to "live in" You can then do something like <button onClick={()=>console.log(allTestTypes)}>click me</button> to view what you have in that new piece of state you just created.
0

Simply you can do multiple await inside one async function

 async function onLoadCreateUnitTests() {
          
   const results = await get('get_tables_autocomplete/b', user.user);
   const allTestTypes = await get('/api/get_all_test_types', user.user);
    
   autoComplete.setTablesAutoComplete(results);

   setAllTestType(allTestTypes); // recommend you to have some state so you can set it here
    
 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.