0

I have a problem with the build application on vercel. After uploading the app on vercel the site works, but I am not able to send a request to the Routes API.

Error looks: TypeError: Failed to parse URL from /api/specialists at Object.fetch (node:internal/deps/undici/undici:11413:11) at async getServerSideProps (/var/task/.next/server/pages/specialists.js:169:22) { page: '/specialists',

API Rote:

import { connectMongo } from '@/helpers/mongodb';

const handler = async (req, res) => {
    const client = await connectMongo();
    const database = client.db();

    if (req.method === 'POST') {
        const specialistData = req.body;

        try {
            await database.collection('specialists').insertOne(specialistData);

            res.status(201).json({ message: 'Specialist Created!' });
        } catch (error) {
            res.status(500).json({ message: 'Cant create a specialist' });
        }
    }

    if (req.method === 'GET') {
        try {
            const specialists = await database
                .collection('specialists')
                .find()
                .toArray();

            res.status(200).json({ specialists });
        } catch (error) {
            res.status(500).json({ message: 'Failed fetch specialists' });
        }
    }

    if (req.method === 'DELETE') {
        const id = req.query.specialists[1];

        try {
            const ObjectId = require('mongodb').ObjectId;
            const convertedObjectId = new ObjectId(id);

            await database
                .collection('specialists')
                .deleteOne({ _id: convertedObjectId });

            res.status(201).json({ message: 'Specialist Deleted!' });
        } catch (error) {
            res.status(500).json({ message: 'Failed to delete specialist' });
        }
    }

    if (req.method === 'PUT') {
        const id = req.query.specialists[1];
        const opinion = req.body;

        try {
            const ObjectId = require('mongodb').ObjectId;
            const convertedObjectId = new ObjectId(id);

            await database
                .collection('specialists')
                .findOneAndUpdate(
                    { _id: convertedObjectId },
                    { $push: { opinions: opinion } }
                );

            res.status(201).json({ message: 'Opinion Added!' });
        } catch (error) {
            res.status(500).json({ message: 'Failed to add opinion' });
        }
    }
};

export default handler;

GetServerSideProps:

export const getServerSideProps = async () => {
    const response = await fetch('/api/specialists');
    const specialists = await response.json();

    return {
        props: { specialists },
    };
};

At the local everything works. I try to switch between SSG and SSR but still didn't work.

0 Answers0