Action expecting two arguments, but context is not passed

This might be a slight misunderstanding from my side about how actions work, but after defining a new action and attempting to call it in the client (it's a create action), it throws an error expecting a second argument (context), but in the examples and elsewhere in the opensaas code I can only see one argument being passed.
12 Replies
kapa.ai
kapa.ai•7mo ago
Hi @CamBlackwood. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- In Wasp, when you define an action, it expects two arguments: args and context. The args argument is an object containing the data passed in when calling the action, while context is an additional context object passed into the action by Wasp. This object contains user session information, as well as information about entities (source (https://wasp-lang.dev/docs/data-model/operations/actions#implementing-actions)). When you call an action from the client, Wasp automatically provides the context for you. Therefore, you only need to pass in the args when calling the action from the client. For example, if you have an action createProject, you would call it like this: createProject({id: 123}). Even though the action expects two arguments (args and context), you only need to provide the args when calling it from the client (source (https://discord.com/channels/686873244791210014/1216119302244270181)). However, if you are calling an action from another action (or from a server-side worker), you need to manually provide the context as the second argument. This is because Wasp does not automatically provide the context in this case. The context you pass should be the same context that was provided to the calling action by Wasp (source (https://discord.com/channels/686873244791210014/1218567315185209464)). Here is an example of how you would call an action from another action:
export const createLastMailMessage: CreateLastMailMessage<void,Mail> = async (_args, context) => {
//...
const { message } = await getLastImapMail(context);
//...
}
export const createLastMailMessage: CreateLastMailMessage<void,Mail> = async (_args, context) => {
//...
const { message } = await getLastImapMail(context);
//...
}
And in backup.ts:
export const getLastImapMail = async (context) => {
//...
const { uploadUrl } = await createFile({ fileType, name }, context);
//...
}
export const getLastImapMail = async (context) => {
//...
const { uploadUrl } = await createFile({ fileType, name }, context);
//...
}
(source (https://discord.com/channels/686873244791210014/1218567315185209464)). If you are still encountering issues, it would be helpful to know the exact error message you are seeing and how you are importing and calling the action. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @CamBlackwood, 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?
CamBlackwood
CamBlackwoodOP•7mo ago
@kapa.ai I'm calling it from the client, and it's defined as an action the same as all other actions - I'll try and run wasp clean to see if that fixes it
kapa.ai
kapa.ai•7mo ago
Let me check the knowledge base for you. One moment! :books: ---------------------- If you're calling the action from the client and it's defined correctly, you shouldn't need to manually pass the context argument. Wasp should handle this for you. However, if you're still encountering issues, running wasp clean could help. This command cleans the .wasp and build directories, which can sometimes resolve issues caused by leftover files from previous builds (source (https://github.com/wasp-lang/wasp/issues/210)). If the problem persists after running wasp clean, it would be helpful to see the exact error message you're getting and the code where you're calling the action. This will help in diagnosing the issue. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @CamBlackwood, 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?
CamBlackwood
CamBlackwoodOP•7mo ago
@kapa.ai I still get the issue, here's the action
export const addCreator: AddCreator<any> = async ({ data }, context) => {
// action
if (!context.user) {
throw new HttpError(401);
}
}
export const addCreator: AddCreator<any> = async ({ data }, context) => {
// action
if (!context.user) {
throw new HttpError(401);
}
}
here is the way I defined it in my main.wasp file
action addCreator {
fn: import {addCreator} from "@src/server/actions.js",
entities: [Creators]
}
action addCreator {
fn: import {addCreator} from "@src/server/actions.js",
entities: [Creators]
}
And here's how I call it in the client
const onSubmit = async (data: any) => {
await addCreator(data);
console.log({ data });
};
const onSubmit = async (data: any) => {
await addCreator(data);
console.log({ data });
};
kapa.ai
kapa.ai•7mo ago
Let me check the knowledge base for you. One moment! :books: ---------------------- Your action and its usage seem to be correct. The context argument is automatically provided by Wasp when you call the action from the client, so you don't need to pass it manually. However, the error you're encountering might be due to the async keyword in your action definition. When you use async, it means the function returns a Promise. If there's no return statement in your function, it will return Promise<undefined>, which might not be what Wasp expects. If your action doesn't perform any asynchronous operations (like database queries), you can remove the async keyword:
export const addCreator: AddCreator<any> = ({ data }, context) => {
// action
if (!context.user) {
throw new HttpError(401);
}
}
export const addCreator: AddCreator<any> = ({ data }, context) => {
// action
if (!context.user) {
throw new HttpError(401);
}
}
If your action does perform asynchronous operations, make sure to return a Promise that resolves to the expected type. For example, if AddCreator is expected to return a Creator, you might do something like this:
export const addCreator: AddCreator<any> = async ({ data }, context) => {
// action
if (!context.user) {
throw new HttpError(401);
}

// Assuming createCreator is a function that creates a new Creator and returns a Promise
return createCreator(data);
}
export const addCreator: AddCreator<any> = async ({ data }, context) => {
// action
if (!context.user) {
throw new HttpError(401);
}

// Assuming createCreator is a function that creates a new Creator and returns a Promise
return createCreator(data);
}
If the problem persists, it would be helpful to see the exact error message you're getting. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @CamBlackwood, 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?
miho
miho•7mo ago
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.
CamBlackwood
CamBlackwoodOP•7mo ago
Thanks @miho - I was using those but missed importing it from operations rather than the actions file. That's sorted the issue 🙂
martinsos
martinsos•7mo ago
Ah yes @CamBlackwood that is a bit tricky, people importing it by mistake from operations! @sodic what are your thoughts on this? I am sure Cam is not the first or last that made this mistake, is there a way we can discourage people from making it? On one hand, we don't want to forbid it completely, as they might want to import stuff directly in some cases, but we do want to also possibly avoid this mistake if possible.
miho
miho•7mo ago
Vite plugin that detects if users tried to import server code on the client? 🙂
martinsos
martinsos•7mo ago
Yeah right -> wait @miho , didn't you do some work in that direction?
miho
miho•7mo ago
I did, but we never did anything more than a POC, we should create an issue and prioritise 😄
martinsos
martinsos•7mo ago
Ok awesome, @miho since you went the further into this :D, can you create an issue for it and also link to this Discord convo?
Want results from more Discord servers?
Add your server