edproton
edproton4w ago

Type inference is not ok. Is assuming the user properties are null

import { query } from "./_generated/server";
import { paginationOptsValidator } from "convex/server";
import { getAll } from "convex-helpers/server/relationships";

export const list = query({
args: { paginationOpts: paginationOptsValidator },
handler: async (ctx, { paginationOpts }) => {
// Fetch a page of tutors
const results = await ctx.db.query("tutors").paginate(paginationOpts);

// Extract user IDs from the tutors
const userIds = results.page.map((t) => t.user);

// Fetch all user documents corresponding to the user IDs
const users = await getAll(ctx.db, userIds); // Everything here is not null

// Combine tutor and user information
const page = await Promise.all(
results.page.map(async (tutor) => {
const user = users.find((u) => u._id === tutor.user); // THIS IS NOT NULLABLE...

// Generate the URL for the user's profile image
const imageUrl = await ctx.storage.getUrl(user!.image);

return {
...tutor,
user: {
...user,
image: imageUrl,
},
};
})
);

return {
...results,
page,
};
},
});
import { query } from "./_generated/server";
import { paginationOptsValidator } from "convex/server";
import { getAll } from "convex-helpers/server/relationships";

export const list = query({
args: { paginationOpts: paginationOptsValidator },
handler: async (ctx, { paginationOpts }) => {
// Fetch a page of tutors
const results = await ctx.db.query("tutors").paginate(paginationOpts);

// Extract user IDs from the tutors
const userIds = results.page.map((t) => t.user);

// Fetch all user documents corresponding to the user IDs
const users = await getAll(ctx.db, userIds); // Everything here is not null

// Combine tutor and user information
const page = await Promise.all(
results.page.map(async (tutor) => {
const user = users.find((u) => u._id === tutor.user); // THIS IS NOT NULLABLE...

// Generate the URL for the user's profile image
const imageUrl = await ctx.storage.getUrl(user!.image);

return {
...tutor,
user: {
...user,
image: imageUrl,
},
};
})
);

return {
...results,
page,
};
},
});
3 Replies
Convex Bot
Convex Bot4w ago
Thanks for posting in <#1088161997662724167>. Reminder: If you have a Convex Pro account, use the Convex Dashboard to file support tickets. - Provide context: What are you trying to achieve, what is the end-user interaction, what are you seeing? (full error message, command output, etc.) - Use search.convex.dev to search Docs, Stack, and Discord all at once. - Additionally, you can post your questions in the Convex Community's <#1228095053885476985> channel to receive a response from AI. - Avoid tagging staff unless specifically instructed. Thank you!
ballingt
ballingt4w ago
A users array's .find is nullable, TypeScript can't prove an array will contain a user that matches. You can add a runtime assertion to fix this:
if (!user) {
throw new Error(`No user found for tutor ${tutor.user}`);
}
if (!user) {
throw new Error(`No user found for tutor ${tutor.user}`);
}
and after those three lines user will no longer be nullable
edproton
edprotonOP4w ago
Thanks a lot @ballingt, fixed the error

Did you find this page helpful?