0

Here's how I tried to do it, but it didn't work

    useEffect(() => {
    try {
        const response = async() => {
            await axios.get('https://prectik-87c20-default-rtdb.europe-west1.firebasedatabase.app/quizes.json')}
        console.log(response.data)
    } catch (e) {
        console.log(e);
    }   
})

And so it should look in class components

    async componentDidMount() {
    try {
        const response = await axios.get('https://prectik-87c20-default-rtdb.europe-west1.firebasedatabase.app/quizes.json')}
        
        console.log(response.data)
    } catch (e) {
        console.log(e);
    }
}

3 Answers 3

1

You are defining your response as a function (that is never used) rather to do a request call . Try to split you request function and the useEffect like this (maybe the useEffect don't permit async functions as its parameter). Maybe this is the correct way to do what you want.

async function request(){
  const response = await axios.get('https://prectik-87c20-default-rtdb.europe-west1.firebasedatabase.app/quizes.json')
  console.log(response.data)
}
useEffect(async () => {
  try {
    request()
  } catch (e) {
     console.log(e);
  }   
})
Sign up to request clarification or add additional context in comments.

1 Comment

useEffect(async () => { try { const response = await axios.get('https://prectik-87c20-default-rtdb.europe-west1.firebasedatabase.app/quizes.json') console.log(response.data) } catch (e) { console.log(e); } }) it won't work because you can't write like that link
0

I believe you forgat to use the response to convert it to a useable data

useEffect(() => {
    try {
        const response = async() => {
        await axios.get('https://prectik-87c20-default-rtdb.europe-west1.firebasedatabase.app/quizes.json')}
        const dataUser = await response.json(); //THIS LINE
        console.log(dataUser)
    } catch (e) {
        console.log(e);
    }   
})

And so it should look in class components

async componentDidMount() {
    try {
        const response = await axios.get('https://prectik-87c20-default-rtdb.europe-west1.firebasedatabase.app/quizes.json')}
        const dataUser = await response.json(); //THIS LINE
        console.log(dataUser)
    } catch (e) {
        console.log(e);
    }
}

I got a few examples in this Github repository

2 Comments

Maybe you're right that in the class component it looks like this, I just go through the old course on React and there it looks like. I tried your solution, but an error occurred link
I will be happy to try and support. I just need the the error Information.
0

you must define a function and call it after

useEffect(() => {
    const fetchData=async()=>{
    try {
        const response = await axios.get('https://prectik-87c20-default-rtdb.europe-west1.firebasedatabase.app/quizes.json')
        console.log(response.data)
    } catch (e) {
        console.log(e);
    }
  };

  fetchData();
})

2 Comments

I tried your solution, but still did not get the desired result. That's what happened to me and what was to come out link
I edit the code I had a problem

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.