Javascript TypeError:尝试将值映射到卡时,无法读取未定义的属性“map”

Javascript TypeError:尝试将值映射到卡时,无法读取未定义的属性“map”,javascript,reactjs,material-ui,Javascript,Reactjs,Material Ui,所以我试着根据一个来自的教程将我的帖子细节映射到卡片上 它给出了TypeError:无法读取未定义的属性“map” 我搜索了stackoverflow和其他网站,但没有人帮我解决这个问题 编译器也给了我这个警告 ./src/components/dummy-posts.js 第1:7行:“posts”被分配了一个值,但从未使用过任何未使用的变量 这是我的密码: App.js 请帮助我。提前谢谢。编译器警告与问题直接相关。这意味着posts被赋予了一个值,但从未被完全使用,因为您定义了posts变

所以我试着根据一个来自的教程将我的帖子细节映射到卡片上

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

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

编译器也给了我这个警告

./src/components/dummy-posts.js

第1:7行:“posts”被分配了一个值,但从未使用过任何未使用的变量

这是我的密码:

App.js


请帮助我。提前谢谢。

编译器警告与问题直接相关。这意味着posts被赋予了一个值,但从未被完全使用,因为您定义了posts变量,但没有对它做任何操作。在JavaScript中,为了像在posts.js中一样导入它,必须在导入的文件中导出它

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

module.exports = { posts };
或者,正如@AtinSingh所建议的,我们可以:

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

在dummy-posts.js中,您是否导出了posts数组?您的posts值未定义。您是否正确导出了它,如果正确,您可以共享它的代码吗。还有console.log你的帖子,请检查一下。谢谢,我不知道我们甚至需要导出常量才能从其他文件中导入它们。这很有效!或者他可以使用es6语法并导出const posts=[];
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"
    }
   ]
module.exports = { posts };
export const posts = [...your array...];