NextJS未显示静态JSON文件中的内容
我接下来是个新手,但这是问题。我有一个静态JSON文件,它是我项目目录的根源,如下所示
{"data":[{"id":1,"attributes":{"name":"Test Product 1","content":"Stvarno ne znam sta bi dodao ovdje jer samo testiram","meta_description":"Opis koji moze da stoji u meti / SEO i to","meta_title":"Testni Product - Lion Kingdom","price":12.99,"slug":"test-product-1","createdAt":"2022-04-17T12:12:36.787Z","updatedAt":"2022-04-17T12:12:39.540Z","publishedAt":"2022-04-17T12:12:39.531Z"}},{"id":2,"attributes":{"name":"Test Product 2","content":"Jos jedan testni post da testiram producte v2","meta_description":"Neki opis za metu","meta_title":"Testni Product 2 - Lion Kingdom","price":49700,"slug":"test-product-2","createdAt":"2022-04-17T12:15:04.241Z","updatedAt":"2022-04-17T12:15:05.078Z","publishedAt":"2022-04-17T12:15:05.075Z"}}],"meta":{"pagination":{"page":1,"pageSize":25,"pageCount":1,"total":2}}}
,我试图将其作为在网站上使用的道具,但由于某些未知原因,编译器没有显示任何错误,但是DIV仍然显示为空的,没有从JSON中获取任何内容。 将其设置
这是我在下一个定义上方定义它
import products from '../products.json'
的方式,然后在体内
export default function index() {
return (
<div>
<Head>
<title> ECommerce Alpha</title>
<link rel="icon" href="/favicon.ico" />
</Head>
{products.data.map(product => {
<div key={product.attributes}>
<div>{product.attributes.name}</div>
</div>
})}
</div>
)
}
为空白页,如果有人可以指出为什么接下来没有从文件中静态地获取它,我会欣赏它:)
I'm relatively new to Next, but here is the issue. I've got a static JSON file which is in the root of my project directory and is as follows
{"data":[{"id":1,"attributes":{"name":"Test Product 1","content":"Stvarno ne znam sta bi dodao ovdje jer samo testiram","meta_description":"Opis koji moze da stoji u meti / SEO i to","meta_title":"Testni Product - Lion Kingdom","price":12.99,"slug":"test-product-1","createdAt":"2022-04-17T12:12:36.787Z","updatedAt":"2022-04-17T12:12:39.540Z","publishedAt":"2022-04-17T12:12:39.531Z"}},{"id":2,"attributes":{"name":"Test Product 2","content":"Jos jedan testni post da testiram producte v2","meta_description":"Neki opis za metu","meta_title":"Testni Product 2 - Lion Kingdom","price":49700,"slug":"test-product-2","createdAt":"2022-04-17T12:15:04.241Z","updatedAt":"2022-04-17T12:15:05.078Z","publishedAt":"2022-04-17T12:15:05.075Z"}}],"meta":{"pagination":{"page":1,"pageSize":25,"pageCount":1,"total":2}}}
Now I'm trying to pass it as a prop to use it on the website, but am not able to for some unknown reason, the compiler shows no errors but yet the div still shows up empty not fetching anything from the json. Here is how I set it up in next
Defining it up top
import products from '../products.json'
and then in the body
export default function index() {
return (
<div>
<Head>
<title> ECommerce Alpha</title>
<link rel="icon" href="/favicon.ico" />
</Head>
{products.data.map(product => {
<div key={product.attributes}>
<div>{product.attributes.name}</div>
</div>
})}
</div>
)
}
The result is yet a blank page, if someone could point out why next is not fetching it statically from a file I would appreciate it :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正在看到一个空白页
您 通过不使用牙套:
You're seeing a blank page because your
map()
's arrow function is missing an explicitreturn
statement:You can make the
return
implicit by not using braces: