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
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:
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?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:
I have this error:
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'
)?Yes this is working, so we can't use the import 'wasp/server/operations' ?
I think @Filip would be able to answer this better than me.
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 😬No, I'm server side, I'm in the stripe webhook
But ok so that's not me, thanks for your reply 🙏