Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

174
Visualizações
For some reason my content within an array is not printing out to the screen

I am currently following a tutorial on PERN stack and whenever I try to print out the contents of an array to the screen to be under title and content, nothing prints out.

Here is my code where the error relies somewhere within:

import React, { Fragment, useState, useEffect } from "react";

const ListBlog = () => {
  const [blogs, setBlogs] = useState([]);

  const getBlogs = async () => {
    const res = await fetch('http://localhost:5000/blog');

    const blogArray = await res.json();

    setBlogs(blogArray)
  }

  useEffect(() => {
    getBlogs();
  }, [])

  console.log(blogs)
  return <Fragment>
    {" "}
    <table className="table mt-5">
    <thead>
      <tr>
        <th>Title</th>
        <th>Content</th>
        <th>Edit</th>
        <th>Delete</th>
      </tr>
    </thead>
    <tbody>
    {/* <tr>
        <td>John</td>
        <td>Doe</td>
        <td>john@example.com</td>
</tr>*/}
     {
       blogs.map(blog => {
         <tr>
           <td>
             {blog.title}
           </td>
           <td>{blog.content}</td>
           <td>Edit</td>
           <td>Delete</td>
         </tr>
       })
     }
    </tbody>
  </table>
  </Fragment>
}

export default ListBlog;

I believe the state is causing errors as it is printing out the following:

(2) [{…}, {…}]0: {post_id: 1, title: 'hi', content: 'howdy'}1: {post_id: 11, title: 'eat', content: 'yogurt'}length: 2[[Prototype]]: Array(0)
[object Object],[object Object]

I just want to print out the array but for some reason [Object object] is printing out as well which may be causing my contents not to print to my localhost page.

Here is what my page looks like when I try to have my contents within the array print underneath title, content, Edit, and Delete but nothing gets displayed...

Nothing displays on screen

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Change curly brackets with normal pharantesis in map

{
       blogs.map(blog => (
         <tr>
           <td>
             {blog.title}
           </td>
           <td>{blog.content}</td>
           <td>Edit</td>
           <td>Delete</td>
         </tr>
       ))
     }
about 4 years ago · Juan Pablo Isaza Relatório

0

this happens because you forget to return in bolgs.map

 {
       blogs.map(blog => {
         return (<tr>
           <td>
             {blog.title}
           </td>
           <td>{blog.content}</td>
           <td>Edit</td>
           <td>Delete</td>
         </tr>)
       })
     }

or

 {
       blogs.map(blog => (
         <tr>
           <td>
             {blog.title}
           </td>
           <td>{blog.content}</td>
           <td>Edit</td>
           <td>Delete</td>
         </tr>
       ))
     }
about 4 years ago · Juan Pablo Isaza Relatório

0

You forgot to return in map

{
   blogs.map(blog => {
     <tr>
       <td>
         {blog.title}
       </td>
       <td>{blog.content}</td>
       <td>Edit</td>
       <td>Delete</td>
     </tr>
   })
}

You can write either this

{
   blogs.map(blog => {
     return (
      <tr>
        <td>
          {blog.title}
        </td>
        <td>{blog.content}</td>
        <td>Edit</td>
        <td>Delete</td>
      </tr>
     )
   })
}

or this

{
  blogs.map(blog => ( 
   <tr>
     <td>
       {blog.title}
     </td>
     <td>{blog.content}</td>
     <td>Edit</td>
     <td>Delete</td>
    </tr>
   ))
 }
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda