Reputation: 1
I am able to successfully create a user and an activity to my Mongo database. However, the activity is not showing up in the user's activity array.
Here is my Prisma schema:
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "mongodb"
url = env("DATABASE_URL")
}
model User {
id String @id @default(auto()) @map("_id") @db.ObjectId
email String @unique
name String?
avatar String? @default("")
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
activities Activity[]
@@map("users")
}
model Activity {
id String @id @default(auto()) @map("_id") @db.ObjectId
// date DateTime
type String
distance String
time String
region String
description String?
user User @relation(fields: [userId], references: [id])
userId String @db.ObjectId
@@map("activities")
}
Here is my Next.js 14 server action function where I successfully create an activity and try to then update the user's activity array to include the newly created activity:
export const createActivityAction = async (
type: string,
distance: string,
time: string,
region: string,
userId: string,
description: string
) => {
try {
const activity = await prisma.activity.create({
data: {
type,
distance,
time,
region,
description,
user: {
connect: {
id: userId,
},
},
},
});
const user = await prisma.user.update({
where: {
id: userId,
},
data: {
activities: {
connect: {
id: activity.id,
},
},
},
});
revalidatePath("/create-activity");
return { activity, user };
} catch (error) {
return { message: "Database Error:Failed to Create Post" };
}
};
I am expecting the activity to show up in the user's activity array so that I can then map over the user's activities on the "my activities" page.
Upvotes: 0
Views: 113