action chaining losing context
I have a action (Defined in my actions.js) that makes a call to an external async call via axios, and when the response is received needs to call another action to save the result to an entity. However, the second action seems to lose context (i.e. the user context is not passed along). What's the best pattern to do something like this?
1 Reply
Hi @cmilazzo. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
When calling an action from another action in Wasp, you need to manually pass the context object to maintain user information. This is a common issue when chaining actions.
The Problem
Based on your description, you're experiencing the issue where the context (particularly the user information) is lost when one action calls another. This happens because when you call an action directly from another action, Wasp doesn't automatically provide the context like it does when calling from the client.
The Solution
The recommended pattern is to pass the context from your first action to the second action. Here's how to do it:
The key is to pass the
context
object as the second argument when calling the second action. This ensures that user information and other context data is preserved.
Important Notes
1. Make sure your action in main.wasp
declares all the entities it needs access to, including those used by the second action Using the context.user
object.
2. When importing the second action, you should import it directly from its file rather than using wasp/client/operations
. For server-side operations, use wasp/server/operations
Discord support thread.
3. If you're not using authentication, you can pass an empty object {}
as the context, but if you need user information, you must pass the full context.
Here's a more complete example:
This pattern ensures that the user context is properly maintained throughout your action chain.
Hope this helps with your action chaining issue!
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------
Sorry @cmilazzo, 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?