Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

154
Vistas
Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'setState')

I am getting this error:

Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'setState')

and this is my code:

class Table extends Component {
    constructor (props) {
        super(props);
        this.state = {
            employees: [],
        }
    }
    componentDidMount() {
        this.getEmployeeList();
    }
    getEmployeeList = () => {
        axios.get("employee/list")
        .then(function(response) {

            this.setState({
                employees: response.data,
            });

        });
        console.log(this.state.employees)
    }
     
    // Remaining codes
    
}
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Here you are passing an anonymous function to the axios.then callback.

axios.get("employee/list")
.then(function(response) {

    this.setState({
        employees: response.data,
    });

});

This function has it's own this that has no setState property on it. To solve this issue, you have to either bind this like so:

axios.then(
  function (response) {
    this.setState({
      employees: response.data
    });
  }.bind(this)
);

Or use an arrow function, which will bind the outer this automatically

axios.then(
 (response) =>{

    this.setState({
      employees: response.data
    });
  }
);
about 4 years ago · Juan Pablo Isaza Denunciar

0

This is because the callback you pass in is a function expression, which has its own this binding.

To solve this error you can:

  1. Use arrow functions:
getEmployeeList = () => {
  axios
    .get("employee/list")
    .then((response) => {
      this.setState({
        employees: response.data,
      });
    });
};
  1. Save this to self variable, and call it instead:
getEmployeeList = () => {
  const self = this;
  axios
    .get("employee/list")
    .then(function (response) {
      self.setState({
        employees: response.data,
      });
    });
};
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda