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

235
Vistas
TypeError: Cannot read properties of undefined (reading 'then')

I have an issue rendering this code in the browser, is there a way I can fix this? If there's more information needed, let me know? I receive the error at the componentDidMount. Is there something I am doing wrong.

The PostService is posted at the bottom.

import React, { Component } from 'react';
import PostService from '../services/PostService';

class ListPost extends Component {
  constructor(props) {
    super(props);
    
    this.state = {
      posts: []
    };
  }
  
  componentDidMount(){
    PostService.getPosts().then((response) => {
      this.setState({ posts: response.data });
    });
  }
  
  render() {
    return (
      <div>
        <h2 className="text-center">Posts</h2>
        <div className="row">
          <table className="table table--striped table-boarded">
            <thead>
              <tr>
                <th>Title</th>
                <th>Description</th>
                <th>Content</th>
              </tr>
            </thead>
            <tbody>
              {
                this.state.posts.map(
                  post => 
                  <tr key={post?.id}>
                    <td>{post?.description}</td>
                    <td>{post?.title}</td>
                    <td>{post?.content}</td>
                  </tr>       
                )}
            </tbody>
          </table>
        </div>
      </div>
    )
  }
}

export default ListPost;


import axios from "axios";

const POST_API_BASE_URL = "http://localhost:8080/api/posts";

class PostService {
    getPosts() {
        axios.get(POST_API_BASE_URL);
    }
}

export default new PostService();
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

First of all change your PostService to;

import axios from "axios";
const POST_API_BASE_URL = "http://localhost:8080/api/posts";

export default function getPosts() {
  return axios.get(POST_API_BASE_URL);
}

And import as import getPosts from '../services/PostService'; on your ListPost class.

Then use the code below. You shouldn't be setting state inside componentDidMount

componentDidMount() {
  this.getData();
}

getData = () => {
  getPosts()
    .then(response => {
      this.setState({ posts: response.data });
    })
    .catch(error => {
      // handle errors here
    })
}
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