Ragpud
Ragpud3w ago

Can I use "skip" with zQuery?

I'm trying to get a files metadata if a file id exists and "skip" would be perfect for that use case. When inspecting useQuery I can pass skip, but when I try it throws an error because of my zod schema in the query definition. Is there a workaround for this?
const fileWithData = useQuery(
api.files.getFile,
stagedImageId
? {
storageId: stagedImageId || "",
}
: "skip"
);
const fileWithData = useQuery(
api.files.getFile,
stagedImageId
? {
storageId: stagedImageId || "",
}
: "skip"
);
zQuery definition
export const zQuery = zCustomQuery(query, NoOp);
export const zQuery = zCustomQuery(query, NoOp);
Error
Argument of type '"skip" | { storageId: string | Id<"_storage">; }' is not assignable to parameter of type '{ storageId: string & { __tableName: "_storage"; }; } | "skip"'.
Type '{ storageId: string | Id<"_storage">; }' is not assignable to type '{ storageId: string & { __tableName: "_storage"; }; } | "skip"'.
Types of property 'storageId' are incompatible.
Type 'string | Id<"_storage">' is not assignable to type '(string & { __tableName: "_storage"; }) | undefined'.
Type 'string' is not assignable to type 'string & { __tableName: "_storage"; }'.
Type 'string' is not assignable to type '{ __tableName: "_storage"; }'
Argument of type '"skip" | { storageId: string | Id<"_storage">; }' is not assignable to parameter of type '{ storageId: string & { __tableName: "_storage"; }; } | "skip"'.
Type '{ storageId: string | Id<"_storage">; }' is not assignable to type '{ storageId: string & { __tableName: "_storage"; }; } | "skip"'.
Types of property 'storageId' are incompatible.
Type 'string | Id<"_storage">' is not assignable to type '(string & { __tableName: "_storage"; }) | undefined'.
Type 'string' is not assignable to type 'string & { __tableName: "_storage"; }'.
Type 'string' is not assignable to type '{ __tableName: "_storage"; }'
2 Replies
Convex Bot
Convex Bot3w 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!
sshader
sshader3w ago
I think this type error might be complaining that stagedImageId || "" has type string which isn't assignable to type Id<"_storage"> (which your function expects). So I'd expect the same type error to happen without the "skip" + once you fix the type error around stagedImageId the "skip" should work fine. You might want to cast stagedImageId to Id<"_storage">, or to have your function accept a string, and then verify that the string is actually a storage ID with ctx.db.normalizeId

Did you find this page helpful?