【问题标题】:Getting null values on POST resquest with strapi deployed on heroku在 heroku 上使用 strapi deploy 获取 POST 请求的空值
【发布时间】:2022-11-17 13:03:50
【问题描述】:

我正在制作一个小网络议程,用户可以在其中编写家庭作业并阅读每天写的作业。 Web 是用 HTML、CSS 和 Vanilla JS 编写的。我有 Strapi 作为后端,我已经使用它的文档将它部署到 Heroku:https://docs.strapi.io/developer-docs/latest/setup-deployment-guides/deployment/hosting-guides/heroku.html#_6-install-the-pg-node-module

我对 GET 请求没有问题,但是当我尝试发出 POST 请求时,我的内容类型的属性得到了空值。这是我的代码(它基于https://docs.strapi.io/developer-docs/latest/developer-resources/database-apis-reference/rest-api.html#create-an-entry):

function registerHomework(){
    var s = document.getElementById("subject").value
    var h = document.getElementById("homework").value
    var t = new Date().toISOString().slice(0, 10)

    fetch('https://agenda-back.herokuapp.com/api/homeworks', {
        method: 'POST',
        headers: {
            'Accept': 'application/json',
            "Content-type": "application/json; charset=UTF-8"
        },
        body: JSON.stringify({
            "data": {
                "id": i,
                "attributes": {subject: `${s}`, homework: `${h}`, date: `${t}`}
            }
             
        })
    })
}

这是我得到的回应:

data: {
    id: 8,
    subject: 'null',
    homework: 'null',
    date: 'null',
    created_at: '2022-11-16T12:12:57.078Z',
    updated_at: '2022-11-16T12:12:57.078Z'
  }

我不知道为什么它不注册属性。我在想这与权限有关,但我已经更改了 strapi 中的内容类型公共角色:现在任何人都应该能够创建一个条目。我仍然得到相同的 null 值。

我还发现了这个:https://github.com/strapi/strapi/issues/5958 但我没有使用 axios。我不知道是否可以直接从前端发出 POST 请求(我从 strapi 文档中了解到它是),但也许我没有正确理解它。也许这是一些授权问题,即使我选择了公共用户可以创建条目?

希望可以有人帮帮我!

【问题讨论】:

    标签: javascript heroku post backend strapi


    【解决方案1】:

    正如您在链接的文档中看到的那样,您不能使用 attributes 键发送数据。您可以直接发送数据。

    {
      "data": {
        "subject": "Hello",
        "homework": "Test",
        "date": "2022-11-17",
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2017-11-07
      • 1970-01-01
      • 1970-01-01
      • 2021-08-02
      • 1970-01-01
      • 1970-01-01
      • 2010-10-15
      • 2020-03-24
      相关资源
      最近更新 更多