are internalMutations only callable from actions?.
From the docs i got this message Internal functions can be called from other functions using the internal object. but the runMutation function is only available in the ctx object of the action handler. Is it supposed to be this way or im i doing something wrong here is a code snippet.
7 Replies
You're doing this right, internal mutations can only be
- called in actions with
ctx.runMutation(internal.file.addNewUserMessage)
- scheduled from actions and mutations with ctx.scheduler.runAfter(internal.file.addNewUserMessage)
- scheduled as cron jobs
- run from the command line with npx convex run file:addNewUserMessage
Thanks mahn. While you are here im running into a tiny bug. Im trying to run an internal mutation when a user signs into my app for the first time. My logic following the docs was to check if a user exists then if not running the internal mutation to the messages table. This logic was working yesterday but right now it isnt. I cant figure it out. Even if the users table is clean it seems the user object is always defined. why is that? here is my logic.
im saving the user by an effect in a react hook.
Would you mind helping me figure why the internalMutation is not running
@Web Dev Cody 😁
@AmohPrince Doesn't seem like you need to use an action, you can expose the mutation directly (use
mutation
instead of internalMutation
). Then make sure that your client code (api.users.storeUser
) matches the name of that mutation.but its an internal mutation dont you think its a bad practice?
Mutations are intended to be exposed as public API to the client.
The Convex approach is:
1. Most things can be accomplished with
query
and mutation
.
2. Only if you need to do something that these can't do (for example, call fetch
to fetch from a third-party API) you reach for action
3. If you need to use a Node package that doesn't work in the default query/mutation/action environment, you use action
with "use node"