W
Wasp-lang10mo ago
kancur

Jobs - Output type error following the example in docs

Hey guys! I'm following the docs on how to create a job (https://wasp-lang.dev/docs/advanced/jobs#job-definition-and-usage). Unfortunately, I'm having some type errors. I'm using openSaas template.
export const foo: MySpecialJob<Input, Output> = async ({ name }, context) => {
console.log(`Hello ${name}!`);
const tasks = await context.entities.Task.findMany({});
return { tasks };
};
export const foo: MySpecialJob<Input, Output> = async ({ name }, context) => {
console.log(`Hello ${name}!`);
const tasks = await context.entities.Task.findMany({});
return { tasks };
};
Error:
Type 'Output' does not satisfy the constraint 'void | JSONValue'.
Type 'Output' is not assignable to type 'JSONObject'.
Property 'tasks' is incompatible with index signature.
Type '(GetResult<{ id: string; description: string; time: string; isDone: boolean; userId: number; createdAt: Date; }, unknown> & {})[]' is not assignable to type 'JSONValue'.ts(2344)
Type 'Output' does not satisfy the constraint 'void | JSONValue'.
Type 'Output' is not assignable to type 'JSONObject'.
Property 'tasks' is incompatible with index signature.
Type '(GetResult<{ id: string; description: string; time: string; isDone: boolean; userId: number; createdAt: Date; }, unknown> & {})[]' is not assignable to type 'JSONValue'.ts(2344)
Is there some issue with the example that I missed? Thank you for your help!
Recurring Jobs | Wasp
In most web apps, users send requests to the server and receive responses with some data. When the server responds quickly, the app feels responsive and smooth.
23 Replies
miho
miho10mo ago
Hey 👋 Have you tried mapping the tasks array into an array of objects with properties you need? If you could be so kind to try this. I'm not sure if the issue is that Prisma's objects are not serializable or we actually have bug.
kancur
kancurOP10mo ago
Hey @miho ! I tried mocking the output completely, with a valid output, but it still complains.
No description
No description
kancur
kancurOP10mo ago
If I set the output type as string, it doesn't complain anymore
No description
kancur
kancurOP10mo ago
type Output = Task[]; // ❌ doesnt work
type Output = Task; // ❌ doesnt work
type Output = { foo: string }; // ✅ works
type Output = string; // ✅ works

type FakeTask = { id: string; name: string; done: boolean };
type Output = { tasks: FakeTask[] }; // ✅ works
type Output = Task[]; // ❌ doesnt work
type Output = Task; // ❌ doesnt work
type Output = { foo: string }; // ✅ works
type Output = string; // ✅ works

type FakeTask = { id: string; name: string; done: boolean };
type Output = { tasks: FakeTask[] }; // ✅ works
MEE6
MEE610mo ago
Wohooo @kancur, you just became a Waspeteer level 1!
kancur
kancurOP10mo ago
So there's something about the Task type, that doesn't work
kancur
kancurOP10mo ago
No description
kancur
kancurOP10mo ago
So as a workaround, I can create a custom type and return that instead of prismas type directly, I guess.
kancur
kancurOP10mo ago
It seems the createdAt field is an issue - when I omit it, it works.
No description
kancur
kancurOP10mo ago
nope, it doesn't actually work, I no longer see the error in vscode, but it doesn't compile 😦
miho
miho10mo ago
Hmmmm that's weird. I guess the issue is that the Task is a class with methods and stuff, have you maybe tried what you said, creating new objects with only the fields you need? e.g.
return {
tasks: tasks.map(task => ({ id: task.id, description: task.description })),
};
return {
tasks: tasks.map(task => ({ id: task.id, description: task.description })),
};
kancur
kancurOP10mo ago
tried it, doesn't help
kancur
kancurOP10mo ago
No description
kancur
kancurOP10mo ago
No description
kancur
kancurOP10mo ago
full error for reference (above)
Property 'createdAt' is incompatible with index signature.
Type 'Date' is not assignable to type 'JSONValue'.
Type 'Date' is not assignable to type 'JSONObject'.
Index signature for type 'string' is missing in type 'Date'.
Property 'createdAt' is incompatible with index signature.
Type 'Date' is not assignable to type 'JSONValue'.
Type 'Date' is not assignable to type 'JSONObject'.
Index signature for type 'string' is missing in type 'Date'.
kancur
kancurOP10mo ago
Here's my task model:
No description
kancur
kancurOP10mo ago
It's basically the OpenSaas template + copy pasting that jobs code from the docs
kancur
kancurOP10mo ago
Okay, I now know why it worked in docs 🙂 The Task entity is much simpler there, and doesn't contain date
No description
MEE6
MEE610mo ago
Wohooo @kancur, you just became a Waspeteer level 2!
kancur
kancurOP10mo ago
if it contained value typed Date, it would throw typescript error like mine I think 🙂
miho
miho10mo ago
Gotcha! Okay, so what's going on here: 1. We are using PgBoss for our async jobs processing 2. PgBoss saves the output of a job in a Postgres database (using a JSON field FYI) 3. PgBoss expects from us to save job outputs as plain objects (Date is a class, not a primitive value like string) which means you can't save really any object you want 4. Ideally, you would save to the database only objects with primitive values, but values you can use to reference the actual task like e.g. Task.id What I would recommend, make the Output more primitive e.g. { id: 5 } and then when you need the tasks, fetch them using the saved IDs
kancur
kancurOP10mo ago
Okay, I see now! 🙂 Thanks for your help & tips @miho !
miho
miho10mo ago
Thank you for the patience and making us figure this out! 🙂 We could improve our jobs output serialization e.g. use Superjson which can handle dates, which would make your experience smoother. Good thing for us to note.
Want results from more Discord servers?
Add your server