Akshay Kadam (A2K)
08/09/2022, 9:41 AMemail
is 3 levels deep?
License
→ Product
→ User
→ email
but i want to query prisma.license.findMany()
& get email
directly.
export const getLicenses = async (): Promise<
Array<License & Pick<User, 'email'>> | null | undefined
> => {
const userSelect = Prisma.validator<Prisma.ProductSelect>()({
user: {
select: {
email: true,
},
},
})
const productSelect = Prisma.validator<Prisma.LicenseSelect>()({
product: {
include: userSelect,
},
})
const licenses = await prisma.license.findMany({
orderBy: {
createdAt: 'desc',
},
include: productSelect,
})
const result = licenses.map((license) => {
const email = license.product?.user.email
if (email) {
return {
...license,
email,
}
}
})
return result
}
i get error on the last line return result
saying:
Type '({ email: string; id: string; name: string; total: number; used: number; productId: string | null; createdAt: Date; product: (Product & { user: { email: string | null; }; }) | null; } | undefined)[]' is not assignable to type '(License & Pick<User, "email">)[]'.
Type '{ email: string; id: string; name: string; total: number; used: number; productId: string | null; createdAt: Date; product: (Product & { user: { email: string | null; }; }) | null; } | undefined' is not assignable to type 'License & Pick<User, "email">'.
Type 'undefined' is not assignable to type 'License & Pick<User, "email">'.
Type 'undefined' is not assignable to type 'License'.ts(2322)
can't find a way to solve it? here's the full question → https://stackoverflow.com/questions/73289404/get-a-nested-email-field-that-is-part-of-another-model-in-prisma & minimal repro → https://github.com/deadcoder0904/prisma-nested-query-emailMichael Jay
08/09/2022, 12:56 PMMichael Jay
08/09/2022, 1:04 PMMichael Jay
08/09/2022, 1:07 PMMichael Jay
08/09/2022, 1:35 PMif (email) {
... do your stuff
};
then you can drop a debug point inside your conditional block and then you'll see exactly what data you have available to you to generate your objects:
if (email) {
acc.push({
id: ...,
email: ...,
....
});
};
Akshay Kadam (A2K)
08/09/2022, 1:44 PMif
. solution → https://stackoverflow.com/a/73292903/6141587Michael Jay
08/09/2022, 1:46 PMAkshay Kadam (A2K)
08/09/2022, 1:48 PMgetLicenses
so i wanted all licenses bcz sometimes there is no email connection as i'm using admin ui to create licensesAkshay Kadam (A2K)
08/09/2022, 1:48 PM