Basscar1408
Basscar14083mo ago

Using Convex query gives Cannot find module '@/convex/_generated/api'

I am working on a Next.js convex project and I am having some trouble with running a query. The project has these dirs app, convex and models. app contains the next js app, and the models has some ts code to interact with ai models in the models dir I am trying to run this query
const activePrompt = await useQuery(api.prompts.getActivePrompt);
const activePrompt = await useQuery(api.prompts.getActivePrompt);
which runs against this table
prompts: defineTable({
promptId: v.id("prompts"),
prompt: v.string(),
isActive: v.boolean(),
}).index("by_active", ["isActive"]),
});
prompts: defineTable({
promptId: v.id("prompts"),
prompt: v.string(),
isActive: v.boolean(),
}).index("by_active", ["isActive"]),
});
and here is the query code
import { query } from "./_generated/server";


export const getActivePrompt = query({
args: {},
handler: async (ctx) => {
return await ctx.db.query("prompts").withIndex("by_active").first();
},
});
import { query } from "./_generated/server";


export const getActivePrompt = query({
args: {},
handler: async (ctx) => {
return await ctx.db.query("prompts").withIndex("by_active").first();
},
});
When I try and call the query from models I get this error
models/index.ts:6:21 - error TS2307: Cannot find module '@/convex/_generated/api' or its corresponding type declarations.

6 import { api } from "@/convex/_generated/api";
~~~~~~~~~~~~~~~~~~~~~~~~~
models/index.ts:6:21 - error TS2307: Cannot find module '@/convex/_generated/api' or its corresponding type declarations.

6 import { api } from "@/convex/_generated/api";
~~~~~~~~~~~~~~~~~~~~~~~~~
and when I try and run it from a prompt.ts file in the convex dir i get
Property 'prompts' does not exist on type '{ auth: { signIn
Property 'prompts' does not exist on type '{ auth: { signIn
Any help would be really appriciated, thanks
5 Replies
Convex Bot
Convex Bot3mo 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!
Clever Tagline
Clever Tagline3mo ago
Your use of the useQuery hook doesn't look correct. It doesn't return a promise, so await isn't necessary. Have you run npx convex dev? This not only connects you to your Convex account, but it also refreshes the generated code during development, and some of the error messages you list make it feel like something didn't get generated.
ballingt
ballingt3mo ago
You might try an example app first, e.g. checking out https://github.com/get-convex/convex-demos and cd-ing to the typescript directory, then npm install and npm run dev There are a few pieces that dont' sound hooked up here, it might help to see them allworking together What are models, is this server code? useQuery is specifically for React components
Clever Tagline
Clever Tagline3mo ago
You should also see Typescript errors in your files if it can't find the resources you're trying to use.
Basscar1408
Basscar1408OP3mo ago
Im also now trying to use it in a mutation and im getting this error
convex/scores.ts:16:39 - error TS2339: Property 'prompts' does not exist on type '{ auth: { signIn: FunctionReference<"action", "public", { provider?: string | undefined; verifier?: string | undefined; refreshToken?: string | undefined; params?: any; calledBy?: string | undefined; }, { ...; }, string | undefined>; signOut: FunctionReference<...>; }; ... 7 more ...; playerresults: { ...; }; }'.

16 const activePrompt = useQuery(api.prompts.getActivePrompt);
~~~~~~~

Found 1 error in convex/scores.ts:16
convex/scores.ts:16:39 - error TS2339: Property 'prompts' does not exist on type '{ auth: { signIn: FunctionReference<"action", "public", { provider?: string | undefined; verifier?: string | undefined; refreshToken?: string | undefined; params?: any; calledBy?: string | undefined; }, { ...; }, string | undefined>; signOut: FunctionReference<...>; }; ... 7 more ...; playerresults: { ...; }; }'.

16 const activePrompt = useQuery(api.prompts.getActivePrompt);
~~~~~~~

Found 1 error in convex/scores.ts:16
this is the mutation
import { v } from "convex/values";
import { internalMutation } from "./_generated/server";
import { useQuery } from "convex/react";
import { api } from "./_generated/api";

export const incrementScore = internalMutation({
args: {
modelId: v.string(),
},
handler: async (ctx, args) => {
const score = await ctx.db
.query("scores")
.filter((q) => q.eq(q.field("modelId"), args.modelId))
.first();

const activePrompt = useQuery(api.prompts.getActivePrompt);
console.log(activePrompt);

if (!score && activePrompt) {
await ctx.db.insert("scores", {
modelId: args.modelId,
promptId: activePrompt?.promptId,
score: 1,
});
} else if (score) {
await ctx.db.patch(score._id, {
score: 1,
});
}
},
});
import { v } from "convex/values";
import { internalMutation } from "./_generated/server";
import { useQuery } from "convex/react";
import { api } from "./_generated/api";

export const incrementScore = internalMutation({
args: {
modelId: v.string(),
},
handler: async (ctx, args) => {
const score = await ctx.db
.query("scores")
.filter((q) => q.eq(q.field("modelId"), args.modelId))
.first();

const activePrompt = useQuery(api.prompts.getActivePrompt);
console.log(activePrompt);

if (!score && activePrompt) {
await ctx.db.insert("scores", {
modelId: args.modelId,
promptId: activePrompt?.promptId,
score: 1,
});
} else if (score) {
await ctx.db.patch(score._id, {
score: 1,
});
}
},
});
and the schema
prompts: defineTable({
promptId: v.id("prompts"),
prompt: v.string(),
isActive: v.boolean(),
}).index("by_active", ["isActive"]),
});
prompts: defineTable({
promptId: v.id("prompts"),
prompt: v.string(),
isActive: v.boolean(),
}).index("by_active", ["isActive"]),
});
I resolved this, by reafactoring the logic and removing the promptId from the prompt table

Did you find this page helpful?