attrapi v3的提取不使用Strapi V4
使用Strapi V3,我的代码完美地工作:
api.loadSource(async actions => {
const { data } = await axios.get('http://localhost:1337/events')
const collection = actions.addCollection({
typeName: 'Event',
path: '/events/:id'
})
for (const event of data) {
collection.addNode({
id: event.id,
})
}
})
但是,随着strapi v4的更新,我会遇到错误: typeError:gridsome.server.js:数据不可能(29:25)
27 | })
28 |
> 29 | for (const event of data) {
| ^
30 | collection.addNode({
31 | id: event.id,
我知道,现在strapi添加 / api /到路径中,默认情况下也不显示关系。但这无法解决情况:
const {data} =等待axios.get('http:// localhost:1337/api/events?pupulate =')
输出当浏览器中输入 http:// http:// localhost:1337/ api/events/1
{
"data": {
"id": 1,
"attributes": {
"title": "Coffe",
"description": "We get together",
"date": "2022-05-30T22:00:00.000Z",
"duration": 2,
"price": 0,
"createdAt": "2022-06-11T12:40:06.542Z",
"updatedAt": "2022-06-11T19:45:53.548Z",
"publishedAt": "2022-06-11T13:05:10.375Z"
}
},
"meta": {
}
}
当输入 http:// localhost:1337/api/events/1?pupulate = **
{
"data": {
"id": 1,
"attributes": {
"title": "Coffe",
"description": "We get together",
"date": "2022-05-30T22:00:00.000Z",
"duration": 2,
"price": 0,
"createdAt": "2022-06-11T12:40:06.542Z",
"updatedAt": "2022-06-11T19:45:53.548Z",
"publishedAt": "2022-06-11T13:05:10.375Z",
"categories": {
"data": [
{
"id": 1,
"attributes": {
"name": "Music",
"createdAt": "2022-06-11T12:44:07.472Z",
"updatedAt": "2022-06-11T12:45:32.721Z",
"publishedAt": "2022-06-11T12:45:32.720Z"
}
}
]
}
}
},
"meta": {
}
}
With Strapi v3 my code perfectly working:
api.loadSource(async actions => {
const { data } = await axios.get('http://localhost:1337/events')
const collection = actions.addCollection({
typeName: 'Event',
path: '/events/:id'
})
for (const event of data) {
collection.addNode({
id: event.id,
})
}
})
But with update to Strapi v4 I'm getting the error: TypeError: gridsome.server.js: data is not iterable (29:25)
27 | })
28 |
> 29 | for (const event of data) {
| ^
30 | collection.addNode({
31 | id: event.id,
I know, that now Strapi added /api/ to the path and also not showing relations by default. But this not fixing the situation:
const { data } = await axios.get('http://localhost:1337/api/events?populate=')
output when enter in browser http://localhost:1337/api/events/1
{
"data": {
"id": 1,
"attributes": {
"title": "Coffe",
"description": "We get together",
"date": "2022-05-30T22:00:00.000Z",
"duration": 2,
"price": 0,
"createdAt": "2022-06-11T12:40:06.542Z",
"updatedAt": "2022-06-11T19:45:53.548Z",
"publishedAt": "2022-06-11T13:05:10.375Z"
}
},
"meta": {
}
}
When enter http://localhost:1337/api/events/1?populate=*
{
"data": {
"id": 1,
"attributes": {
"title": "Coffe",
"description": "We get together",
"date": "2022-05-30T22:00:00.000Z",
"duration": 2,
"price": 0,
"createdAt": "2022-06-11T12:40:06.542Z",
"updatedAt": "2022-06-11T19:45:53.548Z",
"publishedAt": "2022-06-11T13:05:10.375Z",
"categories": {
"data": [
{
"id": 1,
"attributes": {
"name": "Music",
"createdAt": "2022-06-11T12:44:07.472Z",
"updatedAt": "2022-06-11T12:45:32.721Z",
"publishedAt": "2022-06-11T12:45:32.720Z"
}
}
]
}
}
},
"meta": {
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论