gpt4 book ai didi

javascript - Next JS 中预呈现页面时发生错误

转载 作者:行者123 更新时间:2023-12-05 02:41:35 26 4
gpt4 key购买 nike

<分区>

我在 next JS 中创建了一个 API(在 pages/api 文件夹中),并在 pages 文件夹中的页面上使用了它。

当我在localhost(开发阶段)上运行时,可以正确调用API。但是当我部署到 Vercel 时,构建期间出现错误。

build error at vercel

这是我调用 pages/api 文件夹中的 API 时的代码

export const getStaticProps = async () => {
const baseUrlDribble = 'https://api.dribbble.com/v2';
const baseUrl = process.env.NODE_ENV === 'production' ?
'https://jovanka-samudra.vercel.app/api' : 'http://localhost:3000/api';

const resShots = await fetch(`${baseUrlDribble}/user/shots?access_token=${process.env.TOKEN_DRIBBLE}&page=1&per_page=9`);
const shots = await resShots.json();

const resResult = await fetch(`${baseUrl}/projects`);
const result = await resResult.json();
const projects = result.data.projects;

return {
props: {
shots,
projects,
},
revalidate: 1,
}
}

这是从数据库(pages/api/projects 文件夹)检索数据的 API 代码

import ProjectService from "@services/ProjectService";
import connectDB from "@utils/connectDB";
import projectValidator from "@validators/project";
import ClientError from '@exceptions/ClientError';

const handler = async (req, res) => {
const projectService = new ProjectService();

if (req.method === 'GET') {
try {
const projects = await projectService.getProjects();

return res.status(200).json({
success: true,
length: projects.length,
data: {
projects
}
});
} catch (error) {
return res.status(500).json({
success: false,
message: error.message,
});
}
} else if (req.method === 'POST') {
...
}

return res.status(404).json({
success: false,
message: 'Method not alowed'
});
}

export default connectDB(handler);

服务/ProjectService 文件夹

import InvariantError from '@exceptions/InvariantError';
import NotFoundError from '@exceptions/NotFoundError';
import Project from '@models/Project';

class ProjectService {
async getProjects() {
const projects = await Project.find().sort({ 'createdAt': -1 });

return projects;
}

...
}

export default ProjectService;

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com