useQuery throwing errors
Argument of type 'FunctionReference<"mutation", "public", { bio: string; stack: string; links: string; }, null>' is not assignable to parameter of type 'FunctionReference<"query">'.
Type '"mutation"' is not assignable to type '"query"'.
7 Replies
my schema.ts code:
import { defineSchema, defineTable } from "convex/server";
import { v } from "convex/values";
export default defineSchema({
messages: defineTable({
body: v.string(),
user: v.id("users"),
}),
onboard: defineTable({
bio: v.string(),
stack: v.string(),
links: v.string()
}),
users: defineTable({
pictureUrl: v.string(),
name: v.string(),
tokenIdentifier: v.string(),
}).index("by_token", ["tokenIdentifier"]),
});
onboard.ts code:
import { v } from "convex/values";
import { mutation } from "./_generated/server";
export const setOnboarding = mutation({
args: {
bio: v.string(),
stack: v.string(),
links: v.string(),
},
handler: async (ctx, args) => {
await ctx.db.insert("onboard", {
bio: args.bio,
stack: args.stack,
links: args.links,
});
},
});
My frontend code:
const setOnboarding = useQuery(api.onboard.setOnboarding)
The erros:
[{
"resource": "/d:/letscollab/components/verifyForm.tsx",
"owner": "typescript",
"code": "2345",
"severity": 8,
"message": "Argument of type 'FunctionReference<"mutation", "public", { bio: string; stack: string; links: string; }, null>' is not assignable to parameter of type 'FunctionReference<"query">'.\n Type '"mutation"' is not assignable to type '"query"'.",
"source": "ts",
"startLineNumber": 16,
"startColumn": 34,
"endLineNumber": 16,
"endColumn": 59
}]
it's a mutation so you want useMutation, not useQuery
Thanks brother no more errors thanks but can you please explaing why is usemutation and not usequery thanks
useQuery is for queries (readonly, reactive data loading). useMutation is for mutations (read-write, imperative actions)
Check out the docs https://docs.convex.dev/functions/query-functions
Queries | Convex Developer Hub
Queries are the bread and butter of your backend API. They fetch data from the
Mutations | Convex Developer Hub
Mutations insert, update and remove data from the database, check authentication
solved thanks