P
Prisma2mo ago
Manqo

I’m trying to fix an error I’m encountering in my Prisma code: This expression is not callable.

What im trying to accomplish is that when user creates an new record, we give it highest sortOrder. The error occurs in the findFirst function, and here's the message I’m seeing:
Find the first WorkExperience that matches the filter. Note, that providing undefined is treated as the value not being there. Read more here: https://pris.ly/d/null-undefined
Find the first WorkExperience that matches the filter. Note, that providing undefined is treated as the value not being there. Read more here: https://pris.ly/d/null-undefined
Additionally, I'm getting another error at the create function, which reads:
This expression is not callable.
Each member of the union type '(<T extends WorkExperienceCreateArgs<DefaultArgs>>(args: SelectSubset<T, WorkExperienceCreateArgs<DefaultArgs>>) => Prisma__WorkExperienceClient<...>) | ... 5 more ... |
This expression is not callable.
Each member of the union type '(<T extends WorkExperienceCreateArgs<DefaultArgs>>(args: SelectSubset<T, WorkExperienceCreateArgs<DefaultArgs>>) => Prisma__WorkExperienceClient<...>) | ... 5 more ... |
Here’s the relevant code:
type ModelMappingKeys =
| 'workexperiences'
| 'educations'
| 'languages'
| 'skills'
| 'references'
| 'certifications'
| 'licences';
export async function createExperience(
data: Partial<UnifiedFormType> & { formType: FormType; resumeId: string },
) {
const { formType, ...formData } = data;

const modelMapping = {
workexperiences: db.workExperience,
educations: db.education,
languages: db.language,
skills: db.skill,
references: db.reference,
certifications: db.certification,
licences: db.licence,
} as const;

function isValidFormType(type: FormType): type is ModelMappingKeys {
return type in modelMapping;
}

if (!isValidFormType(formType)) {
throw new Error(`Invalid form type: ${formType}`);
}

const model = modelMapping[formType];

const highestItem = await model.findFirst({
where: { resumeId: data.resumeId },
orderBy: { sortOrder: 'desc' },
});

const nextSortOrder = (highestItem?.sortOrder ?? -1) + 1;

const createdItem = await model.create({
data: {
...formData,
sortOrder: nextSortOrder,
},
});
revalidatePath(`/resume/${data.resumeId}`);
return createdItem;
}
type ModelMappingKeys =
| 'workexperiences'
| 'educations'
| 'languages'
| 'skills'
| 'references'
| 'certifications'
| 'licences';
export async function createExperience(
data: Partial<UnifiedFormType> & { formType: FormType; resumeId: string },
) {
const { formType, ...formData } = data;

const modelMapping = {
workexperiences: db.workExperience,
educations: db.education,
languages: db.language,
skills: db.skill,
references: db.reference,
certifications: db.certification,
licences: db.licence,
} as const;

function isValidFormType(type: FormType): type is ModelMappingKeys {
return type in modelMapping;
}

if (!isValidFormType(formType)) {
throw new Error(`Invalid form type: ${formType}`);
}

const model = modelMapping[formType];

const highestItem = await model.findFirst({
where: { resumeId: data.resumeId },
orderBy: { sortOrder: 'desc' },
});

const nextSortOrder = (highestItem?.sortOrder ?? -1) + 1;

const createdItem = await model.create({
data: {
...formData,
sortOrder: nextSortOrder,
},
});
revalidatePath(`/resume/${data.resumeId}`);
return createdItem;
}
1 Reply
Manqo
ManqoOP2mo ago
Actually the code i provided does not work correctly with the sortOrder, but this one works:
export async function createExperience(
data: Partial<UnifiedFormType> & { formType: FormType; resumeId: string },
) {
const { formType, ...formData } = data;
let createdItem;
const getHighestSortOrder = async (model: any) => {
const highestItem = await model.findFirst({
where: { resumeId: data.resumeId },
orderBy: { sortOrder: 'desc' },
});
return highestItem?.sortOrder ?? -1; //
};

const assignNextSortOrder = async (model: any) => {
const highestSortOrder = await getHighestSortOrder(model);
return highestSortOrder + 1;
};
switch (formType) {
case 'workexperiences':
createdItem = await db.workExperience.create({
data: {
...formData,
sortOrder: await assignNextSortOrder(db.workExperience),
},
});
break;
case 'educations':
createdItem = await db.education.create({
data: {
...formData,
sortOrder: await assignNextSortOrder(db.education),
},
});
break;
case 'languages':
createdItem = await db.language.create({
data: {
...formData,
sortOrder: await assignNextSortOrder(db.language),
},
});
break;
case 'licences':
createdItem = await db.licence.create({
data: {
...formData,
sortOrder: await assignNextSortOrder(db.licence),
},
});
break;
default:
throw new Error('Invalid form type');
}

revalidatePath(`/resume/${data.resumeId}`);
return createdItem;
}
export async function createExperience(
data: Partial<UnifiedFormType> & { formType: FormType; resumeId: string },
) {
const { formType, ...formData } = data;
let createdItem;
const getHighestSortOrder = async (model: any) => {
const highestItem = await model.findFirst({
where: { resumeId: data.resumeId },
orderBy: { sortOrder: 'desc' },
});
return highestItem?.sortOrder ?? -1; //
};

const assignNextSortOrder = async (model: any) => {
const highestSortOrder = await getHighestSortOrder(model);
return highestSortOrder + 1;
};
switch (formType) {
case 'workexperiences':
createdItem = await db.workExperience.create({
data: {
...formData,
sortOrder: await assignNextSortOrder(db.workExperience),
},
});
break;
case 'educations':
createdItem = await db.education.create({
data: {
...formData,
sortOrder: await assignNextSortOrder(db.education),
},
});
break;
case 'languages':
createdItem = await db.language.create({
data: {
...formData,
sortOrder: await assignNextSortOrder(db.language),
},
});
break;
case 'licences':
createdItem = await db.licence.create({
data: {
...formData,
sortOrder: await assignNextSortOrder(db.licence),
},
});
break;
default:
throw new Error('Invalid form type');
}

revalidatePath(`/resume/${data.resumeId}`);
return createdItem;
}
But as u can this implementation is not so clean. But anyway i get still the same problem, i dont know what type i should put to the any.
const getHighestSortOrder = async (model: any) => {


const assignNextSortOrder = async (model: any) => {
const getHighestSortOrder = async (model: any) => {


const assignNextSortOrder = async (model: any) => {
So what type i should put here instead of any?
Want results from more Discord servers?
Add your server