setAppMetadata & setUserMetadata not working

Hi! I am currently trying to create an action which is Post Registration and creates some App Metadata for the user. However, even if I break down my code into just these few lines:

exports.onExecutePostUserRegistration = async (event, api) => {

api.user.setAppMetadata(“key”, “value”);

};

It still throws the following error:

“Cannot read properties of undefined (reading ‘setAppMetadata’)”

I already have searched the forums but I’m still unable to find a solution. Any help is very appreciated! Thank you so much in advance!!

Hey @anonetics welcome to the community!

Sorry for the delayed response here - As noted in the following post, adding app and or user metadata to users should be done in pre registration or post login actions:

Hi maximtimeclock Wrote to @anonetics,

The error message “Cannot read properties of undefined (reading ‘setAppMetadata’)” typically indicates that the api.user object or its methods are not defined or accessible in the context where you’re trying to use them.

Ensure that you are using the correct syntax and method for setting app metadata. Ensure that your api object is properly initialized and that the user-related methods (setAppMetadata, setUserMetadata, etc.) are accessible in the context of your function. Check the structure of the event object being passed to your function. Different platforms or environments might have specific requirements or configurations for accessing and using user metadata APIs. Implement error handling to catch and log any errors that occur during the execution of your function.
Use console.log statements to debug and check the availability of api.user and its methods.
Verify if there are any async patterns or promises that need to be properly handled in your function.
Based on the error message you provided, it seems that api.user is undefined when you’re trying to access setAppMetadata. Ensuring proper initialization and understanding of the API usage for setting app metadata should help resolve this issue.