yatahaze
yatahaze
PPrisma
Created by yatahaze on 6/15/2024 in #help-and-questions
TS Type Issue on Nested Selects
Error, console logs, model and query are below. Query returns data as expected. Thanks.
userData?.cart?.cartItems // Property 'cartItems' does not exist on type '{ id: string; userId: string | null; expiresAt: Date | null; createdAt: Date; updatedAt: Date; }'.ts(2339)
userData?.cart?.cartItems // Property 'cartItems' does not exist on type '{ id: string; userId: string | null; expiresAt: Date | null; createdAt: Date; updatedAt: Date; }'.ts(2339)
{
userData: {
deliveryAddresses: [ [Object] ],
billingAddresses: [ [Object] ],
cart: { id: '5e74b112-a088-44b1-ab7e-e10405fe37ac', cartItems: [Array] }
}
}
{
userData: {
deliveryAddresses: [ [Object] ],
billingAddresses: [ [Object] ],
cart: { id: '5e74b112-a088-44b1-ab7e-e10405fe37ac', cartItems: [Array] }
}
}
model Cart {
id String @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid
userId String? @unique @map("user_id") @db.Uuid
expiresAt DateTime? @map("expires_at")
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @updatedAt @map("updated_at")

user User? @relation(fields: [userId], references: [id], onDelete: Cascade)

cartItems CartItem[]

@@index([userId], name: "user_id_idx")
@@map("carts")
}

model CartItem {
cartId String @map("cart_id") @db.Uuid

// ...

cart Cart @relation(fields: [cartId], references: [id], onDelete: Cascade)

// ...
}
model Cart {
id String @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid
userId String? @unique @map("user_id") @db.Uuid
expiresAt DateTime? @map("expires_at")
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @updatedAt @map("updated_at")

user User? @relation(fields: [userId], references: [id], onDelete: Cascade)

cartItems CartItem[]

@@index([userId], name: "user_id_idx")
@@map("carts")
}

model CartItem {
cartId String @map("cart_id") @db.Uuid

// ...

cart Cart @relation(fields: [cartId], references: [id], onDelete: Cascade)

// ...
}
const select: Prisma.UserSelect = {
...(deliveryAddressId && {
deliveryAddresses: {
where: {
id: deliveryAddressId,
},
},
}),

billingAddresses: {
where: {
id: billingAddressId,
},
},

cart: {
select: {
id: true,

cartItems: {
select: {
quantity: true,
product: {
select: {
id: true,
websitePrice: true,
},
},
},
},
},
},
};

const userData = await prisma.user.findUnique({
relationLoadStrategy: 'join',

where: {
id: user.id,
},

select,
});
const select: Prisma.UserSelect = {
...(deliveryAddressId && {
deliveryAddresses: {
where: {
id: deliveryAddressId,
},
},
}),

billingAddresses: {
where: {
id: billingAddressId,
},
},

cart: {
select: {
id: true,

cartItems: {
select: {
quantity: true,
product: {
select: {
id: true,
websitePrice: true,
},
},
},
},
},
},
};

const userData = await prisma.user.findUnique({
relationLoadStrategy: 'join',

where: {
id: user.id,
},

select,
});
3 replies