Next.js Discord

Discord Forum

need help with this error

Unanswered
Schneider’s Smooth-fronted Caima… posted this in #help-forum
Open in Discord
Schneider’s Smooth-fronted CaimanOP
why do i get an error with id

prisma schema
generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider  = "postgresql"
  url       = env("DATABASE_URL")
  directUrl = env("DIRECT_URL")
}
        
model Profile {
  id String @id @default(uuid())
  userId String @unique
  name String
  imageUrl String @db.Text
  email String @db.Text

  servers Server[]
  members Member[]
  channels Channel[]

  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
}

model Server {
  id String @id @default(uuid())
  name String
  imageUrl String @db.Text
  inviteCode String @db.Text

  profileId String
  profile Profile @relation(fields: [profileId], references: [id],onDelete: Cascade)

  members Member[]
  channels Channel[]

  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt

  @@index([profileId])
}

enum MemberRole {
  ADMIN
  MODERATOR
  GUEST
}

model Member {
  id String @id @default(uuid())
  role MemberRole @default(GUEST)

  profileId String
  profile Profile @relation(fields: [profileId],references: [id],onDelete: Cascade)

  serverId String
  server Server @relation(fields: [serverId],references: [id], onDelete: Cascade)

  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt

  @@index([profileId])
  @@index([serverId])
}

enum ChannelType {
  TEXT
  AUDIO
  VIDEO
}

model Channel {
  id String @id @default(uuid())
  name String
  type ChannelType @default(TEXT)

  profileId String
  profile Profile @relation(fields: [profileId],references: [id],onDelete: Cascade)

  serverId String
  server Server @relation(fields: [serverId],references: [id], onDelete: Cascade)

  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt

  @@index([profileId])
  @@index([serverId])
}

37 Replies

Schneider’s Smooth-fronted CaimanOP
initial-profile.ts

import { currentUser, redirectToSignIn } from "@clerk/nextjs/server";
import {db} from "@/lib/db";

export const initalProfile = async () => {
    const user = await currentUser();

    if(!user) {
        return redirectToSignIn();
    }

    const profile = await db.profile.findUnique({
        where:{
            userId: user.id
        }
    });

    if(profile) {
        return profile;
    }

    const newProfile = await db.profile.create({
        data: {
            userId: user.id,
            name: `${user.firstName} ${user.lastName}`,
            imageUrl: user.imageUrl,
            email: user.emailAddresses[0].emailAddress
        }
    });

    return newProfile;
}
can you perhaps show us what's the error?
Schneider’s Smooth-fronted CaimanOP
import { db } from "@/lib/db";
import { initalProfile } from "@/lib/initial-profile";
import { UserButton } from "@clerk/nextjs";
import { redirect } from "next/navigation";

const SetUpPage = async () => {
  const profile = await initalProfile();

  const server = await db.server.findFirst({
    where: {
      members: {
        some: {
          profileId: profile.id,
        },
      },
    },
  });

  if (server) {
    return redirect(`/servers/${server.id}`);
  }
  return (
    <div>
      <UserButton />
    </div>
  );
};

export default SetUpPage;
this is the inital profile
"use client";
import { currentUser, redirectToSignIn } from "@clerk/nextjs/server";
import { db } from "@/lib/db";
import { RedirectToSignIn } from "@clerk/nextjs";

export const initalProfile = async () => {
  const user = await currentUser();

  if (!user) {
    return RedirectToSignIn;
  }

  const profile = await db.profile.findUnique({
    where: {
      userId: user.id,
    },
  });

  if (profile) {
    return profile;
  }

  const newProfile = await db.profile.create({
    data: {
      userId: user.id,
      name: `${user.firstName} ${user.lastName}`,
      imageUrl: user.imageUrl,
      email: user.emailAddresses[0].emailAddress,
    },
  });

  return newProfile;
};
ping me on reply
RedirectToSignIn is a component, I don't think it should be called in a function
Schneider’s Smooth-fronted CaimanOP
it seems to work
pls ping me
okay, if it's working then what's the issue? @Schneider’s Smooth-fronted Caiman
Schneider’s Smooth-fronted CaimanOP
import { db } from "@/lib/db";
import { initalProfile } from "@/lib/initial-profile";
import { UserButton } from "@clerk/nextjs";
import { redirect } from "next/navigation";

const SetUpPage = async () => {
  const profile = await initalProfile();

  const server = await db.server.findFirst({
    where: {
      members: {
        some: {
          profileId: profile.id,
        },
      },
    },
  });

  if (server) {
    return redirect(`/servers/${server.id}`);
  }
  return (
    <div>
      <UserButton />
    </div>
  );
};

export default SetUpPage;
why does initalProfile even has 'use client'. I don't think it's supposed to have it :thinq:
Schneider’s Smooth-fronted CaimanOP
it doesnt work without that
then you are simply doing it wrong, what even are you trying to do?
Schneider’s Smooth-fronted CaimanOP
i m using this to get the data of the inital user from the db
im following a tutorial for a discord clone
but some of his funcations are depricated
so i have to figure a lot of it out myself
so, after login you are trying to store the user data into your db if it doesn't already exists?
Schneider’s Smooth-fronted CaimanOP
yes
i just dont get why intialprofile is not a fucntion
its clearly a function
If I am not wrong it isn't, it's a client component. I don't think you are supposed to add 'use client' to the function, it can be only added to the components.
I am not sure tho
Schneider’s Smooth-fronted CaimanOP
intresting
okk so i should focus on this error i suppose
Yeah
Schneider’s Smooth-fronted CaimanOP
yea i need to see how to use Redirect function
ig rest will be fine
noice