问题描述
我正在做下一个项目,我有一个内容丰富的服务器。 我可以在本地完美地将条目发布到 contentful 但在 vercel 上我收到错误: 0 处 json 位置的意外标记 I
这是我发布条目的代码 index.js 中的代码
const handleSubmitPotato = async (data) => {
data.mouth = assets.mouth;
data.pant = assets.pant;
data.eye = assets.eye;
data.nose = assets.nose;
data.hat = assets.hat;
const response = await fetch("/api/sendpotatoe",{
method: "POST",body: JSON.stringify(data),headers: {
"Content-Type": "application/json",},});
// console.log(response);
await response.json();
console.log(response);
};
api/sendpotato.js 中的代码
const contentful = require("contentful-management");
export default async (req,res) => {
// console.log(req.body);
// console.log(req.body.mouth);
const mouthid = req.body.mouth.sys.id;
const hatid = req.body.hat.sys.id;
const eyeid = req.body.eye.sys.id;
const noseid = req.body.nose.sys.id;
const pantid = req.body.pant.sys.id;
if (req.method === "POST") {
try {
const client = contentful.createClient({
accessToken: `${process.env.CONTENTFUL_MANAGEMENT_TOKEN}`,});
let space = await client.getSpace(process.env.CONTENTFUL_SPACE_ID);
let environment = await space.getEnvironment("master");
const response = await environment.createEntry("potatoes",{
fields: {
name: {
"en-US": req.body.name,potatoname: {
"en-US": req.body.potatoname,message: {
"en-US": req.body.message,mouth: {
"en-US": {
sys: {
id: mouthid,type: "Link",linkType: "Entry",hat: {
"en-US": {
sys: {
id: hatid,eye: {
"en-US": {
sys: {
id: eyeid,nose: {
"en-US": {
sys: {
id: noseid,pant: {
"en-US": {
sys: {
id: pantid,});
await response.publish();
await res.status(200).send({ status: "OK" });
return response;
} catch (err) {
console.log(`${err}`);
res.status(500).end(`Something went wrong: ${e}`);
}
}
};
为什么我在 Vercel 上而不是在本地得到这个? 我已经把我所有的环境变量放在 vercel 上。 我已经放置了 res.status(500).end 并且我认为这会有所帮助。
在我的 vercel 回复选项卡下,它只是说: 内部服务器错误。
我知道这个问题问得很多,但我发现调试起来非常困难。 感谢您的帮助,所有帮助我理解的提示都是一种帮助!
解决方法
我已经找到了如何调试它。我查看了 vercel 上的功能日志,发现我在环境变量中复制粘贴了错误的令牌。感谢那些看过我的帖子或想出答案的人
(我已经检查了我的标题,谢谢@serraosays)