PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, October 12, 2022

[FIXED] Why am I am Getting a Typeerror: res.send is not a function with Axios. Res.send works outside the res.send but I need the data from the API

 October 12, 2022     axios, express, node.js, reactjs     No comments   

Issue

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?


Solution

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



Answered By - srx
Answer Checked By - Clifford M. (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing