0

I have 2 components, A and B. A passes 2 data points to B through props, and inside B I am using these props to make an API call. However this throws an error: "TypeError: data.forEach is not a function". Once the error message is displayed in the console, the results which are supposed to be populated in the react table also get logged, after few moments. I am not able to understand where I am going wrong. Any help is appreciated!

const TableT = (props) => {
    const [data, setData] = useState([])
    async function fetchTableData(t, d){
        await Aios.get("http://localhost:4000/api?dataset="+d+"/table="+t).then((res) => {
            console.log(res.data)
            return res.data
        })
    }
    useEffect(() => {
        let d = fetchTableData(props.dataset, props.table)
        setData(d)
    }, [data])

    //Here comes the standard code of react table, rendered using the "data" state
}

My belief is that the table is getting rendered before we have the data through the API call, hence the error(I could be wrong). Happy to clarify anything if needed!

4
  • Have you tried changing this part http:localhost to http://localhost? That assumes Aios is the correct variable name for your axios lib. There are other problems with your code to be sure, but that's where I would start. Commented Jan 20, 2022 at 20:39
  • Hi...apologies, you are right. It is http://localhost only. And Aios is the correct variable name. I am getting the response from API, but the data is getting logged only after the error is logged. Commented Jan 20, 2022 at 20:47
  • 1
    This let d = fetchTableData(props.dataset, props.table) won't get anything back because the fetchTableData function doesn't return anything. Try testing it by putting console.log(d); right before setData(d). I bet you will get a promise back, which is not what you expect. Commented Jan 20, 2022 at 23:06
  • yes, you are right! Commented Jan 21, 2022 at 6:26

1 Answer 1

1

Your use of async/await is a bit unorthodox here and, frankly, unnecessary. Here is how I would correct your code:

const TableT = (props) => {
    const [data, setData] = useState([])
    const fetchTableData = (t, d) => {
        Aios.get("http://localhost:4000/api?dataset="+d+"/table="+t).then((res) => {
            console.log(res.data)
            setData(res.data);
        });
    }
    useEffect(() => {
        fetchTableData(props.dataset, props.table);
    }, [])

    //Here comes the standard code of react table, rendered using the "data" state
}

This assumes, of course, that props.dataset, props.table are properly passed and your axios call actually returns the data.

There is no need to await for the response of the axios call, you can just get it inside the then and set the state variable right there.

Sign up to request clarification or add additional context in comments.

Comments

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.