[英]NextJS api route works perfectly locally (creates 200+ records) and it's broken on Vercel
This is my API route:这是我的 API 路由:
And the code:和代码:
import type { NextApiRequest, NextApiResponse } from "next";
import axios from "axios";
import prisma from "../../../lib/prisma";
interface Platform {
id: number;
abbreviation: string;
alternative_name: string;
category: number;
created_at: number;
generation: number;
name: string;
platform_logo: {
id: number;
alpha_channel: boolean;
animated: boolean;
height: number;
image_id: string;
url: string;
width: number;
checksum: string;
};
slug: string;
summary: string;
updated_at: number;
url: string;
versions: number[];
checksum: string;
}
export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
const url = "https://api.igdb.com/v4/platforms";
const headers = {
"client-id": process.env.TWITCH_IGDB_CLIENT_ID,
authorization: "Bearer my_token",
"accept-encoding": "*",
};
const body = `fields *, platform_family.*, platform_logo.*;
limit 500;`;
const response = await axios.post(url, body, { headers });
console.log("The response status is:", response.status);
if (response.status == 200) {
await response.data.forEach(async (p: Platform) => {
try {
const platform = await prisma.platform
.create({
data: {
category: p.category,
name: p.name,
slug: p.slug,
platform_logo_url: p.platform_logo?.url, // Todo, sanitize this url.
hashtag: p.name, // Todo, convert name to hashtag
},
});
console.log("Created platforms", platform);
} catch (e) {
console.log(e)
}
});
} else {
console.log(response.status);
}
res.status(200).json({ message: "Imported all platforms." });
}
Locally I hit the url: http://localhost:3000/api/igdb/refresh_platforms and I can see the 199 platforms in my local database.在本地,我点击了 url:http://localhost:3000/api/igdb/refresh_platforms,我可以在我的本地数据库中看到 199 个平台。
On Vercel, I sometimes get 3 inserted, sometimes none inserted, sometimes, 55 inserted.在 Vercel 上,我有时插入 3 个,有时不插入,有时插入 55 个。
It seems completely arbitrary and I'm totally confused.这似乎完全是武断的,我很困惑。
What am I doing wrong here?我在这里做错了什么?
The solution was to use for..of
- that fixed the problem.解决方案是使用
for..of
- 解决了问题。 Thanks @phil谢谢@phil
for (const p of response.data) {
try {
const platform = await prisma.platform.create({
data: {
category: p.category,
name: p.name,
slug: p.slug,
platform_logo_url: p.platform_logo?.url, // Todo, sanitize this url.
hashtag: p.name, // Todo, convert name to hashtag
},
});
console.log("[igdb] Platform created", platform);
} catch (e) {
console.log("[igdb] Platform already exists, skipping", p.name);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.