what's the proper way to query an SDK client in node env?
whenever a user navigates to the billing page, i want to check stripe if they have cancelled the pay, essentially, im trying to do
since stripe SDK is in the node env, i need to wrap it in an "action"
and i was thinking that i could do
and on the page.tsx, do a fetch to my API above. but im running into
or is there a better approach to what im trying to achieve? thanks!
8 Replies
convex's three types of functions are queries, mutations, and actions. Since this is an action, you would call
convex.action
instead of convex.query
got it, thanks! would u say this is the easiest way to do what i try to accomplish above?
apparently useAction requiries something to trigger it, so i'd need to make it live in the api route
Can you share more of the flow? As written, the /api/stripe route seems a little redundant since the client can call the action directly
the goal is to check if the customer has cancelled the subscription plan. when my page renders, im currently doing
hence, i needed to add the route at api/stripe
yeah, you can say something like
const myAction = useAction(api.stripe.actions.getStripePlan);'
const plan = myAction();
in your app
the path /api/stripe/route.ts
implies this is a vercel function. you don't need a seprate vercel function, you can just directly invoke actions from your app using the above syntaxour demo project
giphy-action
shows this. here's the action definition: https://github.com/get-convex/convex-demos/blob/main/giphy-action/convex/messages.ts#L24 and here's the app binding the action hook ( https://github.com/get-convex/convex-demos/blob/main/giphy-action/src/App.tsx#L10 ) and calling the action at the appropriate time and getting the result from the convex backend ( https://github.com/get-convex/convex-demos/blob/main/giphy-action/src/App.tsx#L18 )GitHub
convex-demos/giphy-action/convex/messages.ts at main ยท get-convex/c...
Demo apps built on Convex. Contribute to get-convex/convex-demos development by creating an account on GitHub.
GitHub
convex-demos/giphy-action/src/App.tsx at main ยท get-convex/convex-d...
Demo apps built on Convex. Contribute to get-convex/convex-demos development by creating an account on GitHub.
so, based on your code snippet, you can just call the action in your
useEffect
directly without the vercel function
useAction() doesn't directly invoke the action, it gives you a handle to the action you can call as many times as you want
small rewrite of yours:
got it, makes sense. thanks! yes i can confirm it's working ๐