attrapi v3的提取不使用Strapi V4

发布于 2025-02-06 12:56:56 字数 2071 浏览 0 评论 0原文

使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文