获取错误 - 请求正文中缺少 \"data\" 负载

Fetch error - Missing \"data\" payload in the request body

GraphQL 和 Strapi API 已更改,它们向 json 对象添加了父级别,其中必须获取的整个 JSON 对象必须有一个名为 data 的父键,如果您在没有此键的情况下提交请求,API 将被拒绝并返回 400 错误。

我的JSON我提交的是这样的

{"title": "aaa", "rating": "3", "body": "aa", "categories": "5"}

api需要这样

{"data" : {"title": "aaa", "rating": "3", "body": "aa", "categories": "5"}}

如何调整我的代码以便在此 JSON 对象中插入父键?

使用 Postman,我可以 post Strapi 中的数据,方法是像这样提交数据:

{ "data": {
    "title": "the best car",
    "rating": 7,
    "body": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt. ",
    "categories": [3,7,4]

    }
}

我的完整代码如下:

import React, { useState } from "react";
import { useNavigate } from "react-router-dom";
import { useQuery, gql } from '@apollo/client'
import { useParams, Link } from 'react-router-dom'

const CATEGORIES = gql`
  query GetCategories {
    categories{
      data
      {
        id
        attributes{
          name
        }
      }
    }
  }
`

const token ="AlaBala"
const Create = () => {
    const [title, setTitle] = useState('');
    const [body, setBody] = useState('');
    const [rating, setRating] = useState(3);
    const [categories, setCategories] = useState(5);
    const history = useNavigate();

    const { loading, error, data } = useQuery(CATEGORIES)

    if (loading) return <p>Loading categories...</p>
    if (error) return <p>`Error! ${error}`</p>

    const handleSubmit = (e) => {
        e.preventDefault();
        const review = { title, rating, body, categories };
        console.log(review)

        fetch('http://localhost:1337/api/reviews/', {
            method: 'POST',
            mode: 'cors',
            headers: { "Content-Type": "application/json",
                "Authorization" : "Token " + token },
            body: JSON.stringify(review)

        })
    }
return (object etc...)

尝试将您的提取调用更改为此,注意正文字段:

fetch('http://localhost:1337/api/reviews/', {
            method: 'POST',
            mode: 'cors',
            headers: { "Content-Type": "application/json",
                "Authorization" : "Token " + token },
            body: JSON.stringify({data:review})

        })