![](/img/trans.png)
[英]I try to create a next.js webapp with vercel, but when i try to run vercel dev on cmd, it gave me this error
[英]"TypeError: Failed to parse URL from (URL).vercel.app/api/getMessages" when building Next.js 13 with TypeScript on Vercel
StackOverflow 社区的朋友们,您好:当我尝试在 Vercel 上使用 TypeScript 构建我的 Next.js 13 应用程序时,我遇到了这个错误“TypeError:无法从 next-chat-lenx51hr5-gregory-buffard.vercel.app/api/getMessages 解析 URL”。 在 localhost:3000 上使用npm run dev
应用程序运行良好,但是当尝试在 Vercel 上构建它时出现错误。 我是 Next 的新手菜鸟,非常感谢任何帮助。 该错误还包含一些子错误(来自 Vercel 的构建日志):
TypeError: Failed to parse URL from next-chat-lenx51hr5-gregory-buffard.vercel.app/api/getMessages
22:34:29.292 at Object.fetch (node:internal/deps/undici/undici:11118:11)
22:34:29.292 at async HomePage (/vercel/path0/next-chat/.next/server/app/page.js:552:18) {
22:34:29.292 [cause]: TypeError [ERR_INVALID_URL]: Invalid URL
您还可以查看下面的getMessages.ts
文件:
import type { NextApiRequest, NextApiResponse } from "next";
import redis from "../../redis";
import { Message } from "../../typings";
type Data = {
messages: Message[];
};
type ErrorData = {
body: string;
};
export default async function handler(
req: NextApiRequest,
res: NextApiResponse<Data | ErrorData>
) {
if (req.method !== "GET") {
res.status(405).json({ body: "Method Not Allowed" });
return;
}
const messagesRes = await redis.hvals("messages");
const messages: Message[] = messagesRes
.map((message) => JSON.parse(message))
.sort((a, b) => b.created_at - a.created_at);
res.status(200).json({ messages });
再次感谢您提供的任何帮助。 此外,如果您需要任何其他文件来了解问题,请随时询问。
我已经通过在开发服务器上托管应用程序而不是将其发送到生产环境来解决这个问题——完全跳过了“构建”错误和问题。 感谢大家的关注和贡献!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.