由于某种原因,我的阵列中的内容没有打印到屏幕上
我目前正在遵循有关Pern堆栈的教程,每当我尝试将阵列的内容打印到屏幕上,以 title 和 content ,都没有打印出来。
这是我的代码中依赖其中某个地方的代码:
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>[email protected]</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;
我相信状态正在引起错误,因为它正在打印出以下内容:
(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]
我只想打印出数组也打印出来,这可能导致我的内容不打印到我的本地主机页面。
这是我的页面在 title , content ,编辑和删除的阵列打印中的内容时的样子,但没有显示任何内容...
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>[email protected]</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...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在地图中更改具有正常伪造的卷曲括号
Change curly brackets with normal pharantesis in map
发生这种情况是因为您忘记返回
bolgs.map
或
this happens because you forget to return in
bolgs.map
or
您忘记了返回在地图中
您可以写这本书
或
You forgot to return in map
You can write either this
or this