API 端点导致 Next.js 应用程序中出现验证错误“文档中缺少文档类型”

问题描述 投票:0回答:1

我正在使用 Sanity studio API 将信息来回发送到我的 next.js 应用程序。当我在浏览器中点击 url API 端点时,它给了我这个:

{"message":"Couldn't submit comment","err":{"response":{"body":{"error":{"description":"the mutation(s) failed: Invalid value: Document type is missing in document \"pORXPiOwSI61R3J9TeKimb\". Value was (<nil>): <nil>","items":[{"error":{"description":"Document type is missing in document \"pORXPiOwSI61R3J9TeKimb\"","type":"validationError","value":{"Kind":{"NullValue":0}}},"index":0}],"type":"mutationError"}},"url":"https://2w2youqk.api.sanity.io/v1/data/mutate/production?returnIds=true&returnDocuments=true&visibility=sync","method":"POST","headers":{"content-type":"application/json; charset=utf-8","content-length":"370","x-ratelimit-remaining-second":"49","x-ratelimit-limit-second":"50","ratelimit-limit":"50","ratelimit-remaining":"49","ratelimit-reset":"1","x-sanity-shard":"gcp-eu-w1-01-prod-1024","x-served-by":"gradient-web-579bd68bbf-dnpdb","date":"Mon, 04 Apr 2022 15:16:01 GMT","vary":"Origin","xkey":"project-2w2youqk, project-2w2youqk-production","via":"1.1 google","alt-svc":"h3=\":443\"; ma=2592000,h3-29=\":443\"; ma=2592000","connection":"close"},"statusCode":400,"statusMessage":"Bad Request"},"statusCode":400,"responseBody":"{\n  \"error\": {\n    \"description\": \"the mutation(s) failed: Invalid value: Document type is missing in document \\\"pORXPiOwSI61R3J9TeKimb\\\". Value was (<nil>): <nil>\",\n    \"items\": [\n      {\n        \"error\": {\n          \"description\": \"Document type is missing in document \\\"pORXPiOwSI61R3J9TeKimb\\\"\",\n          \"type\": \"validationError\",\n          \"value\": {\n            \"Kind\": {\n              \"NullValue\": 0\n            }\n          }\n        },\n        \"index\": 0\n      }\n    ],\n    \"type\": \"mutationError\"\n  }\n}","details":{"description":"the mutation(s) failed: Invalid value: Document type is missing in document \"pORXPiOwSI61R3J9TeKimb\". Value was (<nil>): <nil>","items":[{"error":{"description":"Document type is missing in document \"pORXPiOwSI61R3J9TeKimb\"","type":"validationError","value":{"Kind":{"NullValue":0}}},"index":0}],"type":"mutationError"}}}

所以我调查了错误,类似这样的事情表明标记错误。问题是我在端点中没有任何标记或 CSS。这只是 JavaScript。 createComment 端点如下所示:

import type { NextApiRequest, NextApiResponse } from 'next'
import   sanityClient   from '@sanity/client'

const config = {
  dataset: process.env.NEXT_PUBLIC_SANITY_DATASET,
  projectId: process.env.NEXT_PUBLIC_SANITY_PROJECT_ID,
  useCdn: process.env.NODE_ENV === "production",
  token: process.env.SANITY_API_TOKEN,
};

const client = sanityClient(config);



export default async function createComment(
  req: NextApiRequest,
  res: NextApiResponse
) {
const { _id, name, email, comment } = req.body;



    await client.create({
        _type: comment,
        post: {
            _type: 'reference',
            _ref: _id
        },
        name,
        email,
        comment
    }).then(res => { 
        console.log(req.body, res);
    }).catch((err) => {
        return res.json({ message: "Couldn't submit comment", err})
})


}

这是我访问 slug.tsx 中端点的位置:

const onSubmit: SubmitHandler<IFormInput> = async (data) => {
  

  
       await fetch('/api/createComment', {
           method: 'POST',
           body: JSON.stringify(data)
       }).then(() => {
      console.log(data);
       }).catch(err => {
          console.log(err);
      })
  
  }

当我实际点击网页上的提交按钮时,它不会在浏览器控制台中抛出任何错误。仅当我访问 localhost3000.com/api/createComment 端点时,此错误才会显示在 JSON 中。这阻碍了我向 Sanity Studio 发送博客评论。知道为什么会发生这种情况吗?我将不胜感激任何和所有的反馈。谢谢。

next.js sanity
1个回答
0
投票

我的 API 密钥设置时没有“READ”设置,因此我创建了一个具有适当权限的新密钥。尝试创建一个具有读写权限的新令牌。

© www.soinside.com 2019 - 2024. All rights reserved.