Home > Blockchain >  Why am I am Getting a Typeerror: res.send is not a function with Axios. Res.send works outside the r
Why am I am Getting a Typeerror: res.send is not a function with Axios. Res.send works outside the r

Time:09-28

enter image description here

I need to get data from this api endpoint, and send it to my client (React). Everything works fine between the frontend and backend, but I cant seem to figure out how to get the data within /dailyscores endpoint and send it using axios. Any help on why res.send is not a function inside .then, and a way to get it work?

CodePudding user response:

The way you are using res as argument name for both express and axios callback is the issue here.

app.get('...', (req, res) => {
  axios.get('...').then((res) => {
    res.send(res.data); // here the res of axios.then is used
  })
});

Instead use different names

app.get('...', (req, res) => {
  axios.get('...').then((response) => {
    res.send(response.data);
  })
});

checkout variable scopes for more info

CodePudding user response:

To get data from API please try as below:

const [apiResp, setApiResp] = useState([]);

 useEffect(() => {
    axios.get(`<api>`)
      .then(res => {
        const response = res.data;
        setApiResp([response]);
      })
      
  });
  • Related