Server Error: 配置必须包含 `projectId`

huangapple go评论57阅读模式
英文:

Server Error: Configuration must contain `projectId`

问题

http://localhost:3000/,我收到以下错误消息:

服务器错误
错误:配置必须包含 `projectId`

lib\client.js 包含:

const config = {
  projectId: process.env.NEXT_PUBLIC_SANITY_ID || 'xxxxx',
  dataset: process.env.NEXT_PUBLIC_SANITY_DATASET || 'production',
  apiVersion: '2021-03-25',
  useCdn: true,
}

const client = sanityClient(config)

我已将 CORS 来源添加到我的项目,并且根目录中还有一个 .env.local.example 文件:

NEXT_PUBLIC_SANITY_PROJECT_ID="xxxxxx"
NEXT_PUBLIC_PROJECT_ID="sj830cdx"

(所以不适用于 此答案

如果我将 env.local.example 重命名为 env.localhttp://localhost:3000/ 将会加载为空白。

感谢帮助。

英文:

I have a local repo of a live NextJS/Sanity website, but I've set the Sanity projectID to a different blank Sanity project to avoid overwriting the live website.

At http://localhost:3000/, I'm receiving:

Server Error
Error: Configuration must contain `projectId`

lib\client.js contains:

const config = {
  projectId: process.env.NEXT_PUBLIC_SANITY_ID || 'xxxxx',
  dataset: process.env.NEXT_PUBLIC_SANITY_DATASET || 'production',
  apiVersion: '2021-03-25',
  useCdn: true,
}

const client = sanityClient(config)

I've added a CORS origin to my project & there's also a .env.local.example in the root folder

NEXT_PUBLIC_SANITY_PROJECT_ID="xxxxxx"
NEXT_PUBLIC_PROJECT_ID="sj830cdx"

(so not solved by this answer)

If I rename env.local.example to env.local http://localhost:3000/ will load blank.

Help appreciated.

答案1

得分: 0

似乎你收到的错误消息表明在用于初始化 sanityClientconfig 对象中缺少 projectId 配置值。projectId 是 Sanity 客户端正常工作所必需的配置参数。

要解决这个问题,你可以更新 lib/client.js 中的 config 对象,使用 NEXT_PUBLIC_SANITY_PROJECT_ID

const config = {
  projectId: process.env.NEXT_PUBLIC_SANITY_PROJECT_ID || 'xxxxx',
  dataset: process.env.NEXT_PUBLIC_SANITY_DATASET || 'production',
  apiVersion: '2021-03-25',
  useCdn: true,
}

然后,确保将 .env.local.example 文件重命名为 .env.local,并将 NEXT_PUBLIC_SANITY_PROJECT_ID 的值设置为你 Sanity 项目的正确项目 ID。这应该使 sanityClient 能够正确初始化,并解决你所遇到的服务器错误。

英文:

Seems like the error message you received indicates that the projectId configuration value is missing in the config object used to initialize the sanityClient. The projectId is a required configuration parameter for the Sanity client to work properly.
To fix the issue, you can update the config object in lib/client.js to use NEXT_PUBLIC_SANITY_PROJECT_ID

const config = {
  projectId: process.env.NEXT_PUBLIC_SANITY_PROJECT_ID || 'xxxxx',
  dataset: process.env.NEXT_PUBLIC_SANITY_DATASET || 'production',
  apiVersion: '2021-03-25',
  useCdn: true,
}

Then, make sure to rename your .env.local.example file back to .env.local and set the value of NEXT_PUBLIC_SANITY_PROJECT_ID to the correct project ID for your Sanity project. This should allow the sanityClient to initialize correctly and resolve the server error you were seeing.

答案2

得分: 0

我将"sanity import"更改如下,这对我有效。

import { createClient } from 'next-sanity';

此外,我读到"sanityClient configuration"已被弃用。请使用"createClient"代替。

英文:

I changed the sanity import to as below and it worked for me.
import {createClient) from 'next-sanity';
Also I read that the sanityClient configuration is deprecated. Use createClient instead.

huangapple
  • 本文由 发表于 2023年2月27日 07:15:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/75575587.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定