1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

[error]API resolved without sending a response for ..., this may result in stalled requests.

Last updated at Posted at 2024-03-11

タイトルの通りのエラーが表示されていたが、解決したので忘れないようにメモ

改善前:/api/shop.ts
import prisma from '@/lib/prisma';
import { Shop } from '@prisma/client';
import { NextApiRequest, NextApiResponse } from 'next';

async function createShopHandler(req: NextApiRequest, res: NextApiResponse) {
    const shop: Shop = req.body
    await prisma.shop
        .create({
            data: shop
        })
        .then(async () => {
            res.status(201).json(shop)
        })
        .catch(async (e) => {
            console.error(e)
            res.status(500).json({ error: 'server error' })
        })
        .finally(async () => {
            await prisma.$disconnect();
        })
}
async function findShopsHandler(req: NextApiRequest, res: NextApiResponse) {
    await prisma.shop
        .findMany()
        .then(async (shops) => {
            res.status(200).json(shops)
        })
        .catch(async (e) => {
            console.error(e)
            res.status(500).json({ error: 'server error' })
        })
        .finally(async () => {
            await prisma.$disconnect();
        })
}
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
    switch (req.method) {
        case 'GET':
            findShopsHandler(req, res)
            break;
        case 'POST':
            createShopHandler(req, res)
            break;
        default:
            res.status(405).json({ error: 'Required Method is not allowed.' })
            break;
    }
}
改善後:/api/shop.ts
import prisma from '@/lib/prisma';
import { Shop } from '@prisma/client';
import { NextApiRequest, NextApiResponse } from 'next';

async function createShopHandler(req: NextApiRequest, res: NextApiResponse) {
    const shop: Shop = req.body
    await prisma.shop
        .create({
            data: shop
        })
        .then(async () => {
            res.status(201).json(shop)
        })
        .catch(async (e) => {
            console.error(e)
            res.status(500).json({ error: 'server error' })
        })
        .finally(async () => {
            await prisma.$disconnect();
        })
}
async function findShopsHandler(req: NextApiRequest, res: NextApiResponse) {
    await prisma.shop
        .findMany()
        .then(async (shops) => {
            res.status(200).json(shops)
        })
        .catch(async (e) => {
            console.error(e)
            res.status(500).json({ error: 'server error' })
        })
        .finally(async () => {
            await prisma.$disconnect();
        })
}

export default async function handler(req: NextApiRequest, res: NextApiResponse) {
    switch (req.method) {
        case 'GET':
            return findShopsHandler(req, res)
        case 'POST':
            return createShopHandler(req, res)
        default:
            return res.status(405).json({ error: 'Required Method is not allowed.' })
    }
}

switch利用時にreturnされていないため、requestが終わらないよう〜となっているようだった。

今度詳しく調査予定

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?