gpt4 book ai didi

javascript - 类型错误 : Cannot read property 'map' of undefined - When tried to map values to cards

转载 作者:行者123 更新时间:2023-12-02 22:11:40 27 4
gpt4 key购买 nike

所以我尝试按照 here 的教程将我的帖子详细信息映射到卡片

它给出了“TypeError:无法读取未定义的属性'map'”

我搜索了 stackoverflow 和其他网站,但没有一个网站能帮助我解决这个问题。

编译器也给了我这个警告。

./src/components/dummy-posts.js

Line 1:7: 'posts' is assigned a value but never used no-unused-vars

这是我的代码:

App.js

import React, { Component } from 'react';
import AppBar from '@material-ui/core/AppBar';
import Toolbar from '@material-ui/core/Toolbar'
import TypoGraphy from '@material-ui/core/Typography'
import NavBar from './components/nav'
import Posts from './components/posts'

class App extends Component {
render() {
return (
<div>
<div>
<AppBar color="primary" position="static">
<Toolbar>
<TypoGraphy variant="title" color="inherit">
My header
</TypoGraphy>
<NavBar />
</Toolbar>
</AppBar>
</div>
<Posts/> {/*This is the function which gets the posts component*}
</div>
);
}
}
export default App;

posts.js

import React from "react";
import { Grid,Typography } from "@material-ui/core";
import Card from "@material-ui/core/Card";
import CardActionArea from "@material-ui/core/CardActionArea";
import CardActions from "@material-ui/core/CardActions";
import CardContent from "@material-ui/core/CardContent";
import CardMedia from "@material-ui/core/CardMedia";
import Button from "@material-ui/core/Button";
import { posts } from "./dummy-posts";

function Posts(props) {
return (
<div style={{ marginTop: 20, padding: 30 }}>
<Grid container spacing={40} justify="center">
{posts.map(post => ( {/*Here's where the error occurs*/}
<Grid item key={post.title}>
<Card>
<CardActionArea>
<CardMedia
component="img"
alt="Contemplative Reptile"
height="140"
image={post.image}
title="Contemplative Reptile"
/>
<CardContent>
<Typography gutterBottom variant="h5" component="h2">
{post.title}
</Typography>
<Typography component="p">{post.excerpt}</Typography>
</CardContent>
</CardActionArea>
<CardActions>
<Button size="small" color="primary">
Share
</Button>
<Button size="small" color="primary">
Learn More
</Button>
</CardActions>
</Card>
</Grid>
))}
</Grid>
</div>
);
}

export default Posts;

dummy-posts.js

const posts=[

{
title: "My first post",
excerpt: "This is my first post with more content inside",
image: "random_url"
},

{
title: "My second post",
excerpt: "This is my second post with more content inside",
image: "random_url"
},

{
title: "My third post",
excerpt: "This is my third post with more content inside",
image: "random_url"
},

{
title: "My fourth post",
excerpt: "This is my fourth post with more content inside",
image: "random_url"
},

{
title: "My fifth post",
excerpt: "This is my fifth post with more content inside",
image: "random_url"
},

{
title: "My sixth post",
excerpt: "This is my sixth post with more content inside",
image: "random_url"
}
]

请帮助我。提前致谢。

最佳答案

编译器警告直接与问题相关。这就是说“posts 被分配了一个值但从未使用”,这正是因为您定义了 posts 变量但没有对其执行任何操作。在 JavaScript 中,为了像在 posts.js 中那样导入它,您必须在要导入的文件中导出它。

您必须导出帖子。在 dummy_posts.js 文件末尾,您可以添加:

module.exports = { posts };

或者,正如 @AtinSingh 所建议的,我们可以这样做:

export const posts = [...your array...];

关于javascript - 类型错误 : Cannot read property 'map' of undefined - When tried to map values to cards,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59541809/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com