Using action in backend

Hello! https://wasp-lang.dev/docs/data-model/operations/actions#declaring-actions I see we can use action in the backend. I want to do so to avoid code duplication. How do you use the action in the backend? Do you have an example? In the front I just import it from wasp/client/operations and it works like a charm. But in backend I'm not really sure how to use it?
Actions | Wasp
We'll explain what Actions are and how to use them. If you're looking for a detailed API specification, skip ahead to the API Reference.
7 Replies
kapa.ai
kapa.ai6mo ago
Hi @Estelle. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- Sure, you can definitely use actions in the backend to avoid code duplication. However, the way you import and use actions in the backend is slightly different from the frontend. In the backend, you should import actions from wasp/server/operations instead of wasp/client/operations. Also, when calling an action imported from wasp/server/operations, you need to pass a context that contains the user info. If your action doesn't require authentication, you should be able to pass an empty object as the context. Here is an example of how you might use an action in the backend:
import { createFoo } from 'wasp/server/operations'

const result = await createFoo(args, context)
import { createFoo } from 'wasp/server/operations'

const result = await createFoo(args, context)
In this example, args is the arguments you want to pass to the action, and context is the context object. Please note that the context object is required when calling actions on the server. This is because actions in Wasp are designed to be used in both the client and the server, and the context object provides necessary information for the action to work correctly on the server. For more information, you can refer to the Wasp documentation on actions (https://wasp-lang.dev/docs/data-model/operations/actions#using-actions). Also, there was a discussion on Discord (https://discord.com/channels/686873244791210014/1218567315185209464) about this topic, which you might find helpful. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @Estelle, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist: Perhaps try again in 15mins?
Estelle
EstelleOP6mo ago
As soon as I import my action in a backend, it crash. useAuth.ts:20 GET http://localhost:3001/auth/me net::ERR_CONNECTION_REFUSED Plus, I don't have any safe typsecript:
import { respondToDeal } from "wasp/server/operations";

......

await respondToDeal({ matchingId }, context);
import { respondToDeal } from "wasp/server/operations";

......

await respondToDeal({ matchingId }, context);
I have this error:
(alias) respondToDeal(args: any, context: any): Promise<any>
(alias) respondToDeal(args: any, context: any): Promise<any>
Vinny (@Wasp)
Vinny (@Wasp)6mo ago
Have you just tried importing the function directly (via its relative path, e.g. 'import { yourAction } from '../path/to/actions') rather than by using the wasp import ('wasp/server/operations')?
Estelle
EstelleOP6mo ago
Yes this is working, so we can't use the import 'wasp/server/operations' ?
Vinny (@Wasp)
Vinny (@Wasp)6mo ago
I think @Filip would be able to answer this better than me.
Filip
Filip6mo ago
Hmm, this looks like a network error, did you import wasp/server/oprations on the client by accident? And this is a good question. The answer is: you should be able to use that import, but we have a bug. The fix will soon be released, and everything will work as expected. More details here: https://discord.com/channels/686873244791210014/1246006532148166758/1246391820842434625 Sorry for the inconvenience 😬
Estelle
EstelleOP6mo ago
No, I'm server side, I'm in the stripe webhook But ok so that's not me, thanks for your reply 🙏
Want results from more Discord servers?
Add your server