这是我刚刚在Vercel部署的website.我正在使用Prisma和Next.js构建一个Web应用程序,我遇到了一个问题,在我手动重新部署该应用程序之前,内容不会实时更新.以下是场景:

  • 我的Next.js应用程序中有一个从Prisma数据库获取数据的API端点.
  • 当我通过应用程序在数据库中创建或更新数据时,更改会立即反映在开发环境中,但在我重新部署应用程序之前,它们不会反映在生产环境中.

以下是我在前端获取数据的方式:

const { data: posts, error } = useSWR(`/api/getPosts`, fetcher, {refreshInterval:1000});

这是发布内容的API端点:

// Addposts to prisma backend

import { NextResponse, NextRequest } from 'next/server';
import prisma from '../../../prisma/client';

// Function

export async function POST(request:NextRequest) {
    const data = await request.json();
    const title = data.title;
    const user = await prisma.user.findUnique({
        where: {
            email : data.email
        }
    })
    if (!user){
        // return error
        return NextResponse.json({error: "User not found"}, {status: 404})
    }

    if (!title){
        // throw error
        return NextResponse.json({error: "Title is required"}, {status: 400})
    }

    if (title.length > 300){
        return NextResponse.json({error:"Title should not be more than 300 characters"}, {status:400});
    }
    const userId = user?.id;

    const post = await prisma.post.create({
        data: {
            title,
            userId
        }
    })
    try{
        return NextResponse.json({post},{status:200})
    }catch(error){
        return NextResponse.json({error}, {status:500})
    }
}


获取所有帖子的API端点:

import { NextRequest, NextResponse } from 'next/server'
import prisma from '../../../prisma/client'
import { NextApiResponse } from 'next';


export async function GET(request:NextRequest){
    const posts = await prisma.Post.findMany({
        include: {
            user: true
        },
        orderBy:{
            createdAt: 'desc'
        }
    })
    try{
        // return all the posts
        return NextResponse.json({posts},{status:200})
    }catch(error){
        return NextResponse.json(error, {status:500});
    }
}

我如何才能确保内容更新立即反映在生产环境中,而不需要手动重新部署?

这是GitHub回购的link美元.

UPDATE

I am able to make POST request and make changes to the db, I think the problem has to be with the GET request since the data appears to be static even when I refresh the page.

以下是我在Vercel上的运行时日志(log):

Vercel runtime logs

推荐答案

好的,经过几天的调试,以下是对我有效的方法:

我将GET和POST函数移到了单个route.ts文件中.

Before:

这是我如何完成GET请求的:axios.get(url/api/getPosts) 和我的帖子请求axios.post(url/api/addPosts)

After:

这是我如何完成GET请求的:axios.get(url/api/Posts) 和我的帖子请求axios.post(url/api/Posts)

Node.js相关问答推荐

Spotify Auth访问令牌给出错误代码400

如何修复node.js中的错误代码无法加载资源:服务器响应状态为403(禁止)

与诗乃一起嘲笑奈克斯

MongoDB - 查找查询以判断是否存在少量字段,结合字段上的 AND

为什么要加密 CSRF 令牌?

多字段传递获取查询失败

用户与mongoose 的完美搭配

在 node:readline 中按 CTRL+D 时会发出什么信号?

如果 express.js (node.js) http 请求在完成之前关闭会发生什么?

users.watch(在 gmail google api 中)如何收听通知?

如何在 mongoDB 中进行全动态搜索?

如何更改NodeJS中的堆栈大小限制?

如何在 Node.js 的 console.log() 中创建换行符

等价于 Node.js 的 Rails 控制台

使用 Node.js 在内存中缓冲整个文件

如何使用 mocha.js 模拟用于单元测试的依赖类?

socket.io 发出回调合适吗?

所有的javascript回调都是异步的吗?如果不是,我怎么知道哪些是?

Express.js:没有这样的文件或目录

使用 Node.js 和 Express 进行简单的 API 调用