我正在做下一个项目,我有一个内容丰富的服务器。我可以在本地完全地将一个条目发布到contentful,但是在vercel上,我得到了错误:在json位置为0的意外令牌i。
下面是我在index.js中发布条目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,
type: "Link",
linkType: "Entry",
},
},
},
eye: {
"en-US": {
sys: {
id: eyeid,
type: "Link",
linkType: "Entry",
},
},
},
nose: {
"en-US": {
sys: {
id: noseid,
type: "Link",
linkType: "Entry",
},
},
},
pant: {
"en-US": {
sys: {
id: pantid,
type: "Link",
linkType: "Entry",
},
},
},
},
});
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的“响应”选项卡下,它只是说:内部服务器错误。
我知道这个问题被问了很多次,但我发现很难调试。谢谢你的帮助,所有的提示都能帮助我理解这是一个很好的帮助!
发布于 2021-04-28 07:03:32
我已经找到了如何调试它。我查看了vercel上的功能日志,发现在我的环境中粘贴了错误的标记。谢谢你们来看我的帖子或想出答案。
(我已经检查了我的标题,谢谢@serraosays )
https://stackoverflow.com/questions/67280038
复制相似问题