User or App Metadata was not Updated in Actions

Problem statement

The Action code assigns a value to event.user.user_metadata[“hoo”].

console.log(event.user.user_metadata["hoo"]) // Outputs "Original Value"
event.user.user_metadata["hoo"] = "Updated Value"

However, the following Actions do not get this updated value.

// In the next Action...
console.log(event.user.user_metadata["hoo"]) // Still outputs "Original Value"

The same goes for event.user.app_metadata[“hoo”].

Steps to reproduce

  1. Create 2 Post Login Actions. Say Action 1 and 2. And add them to the flow.
  2. In Action 1, assign a value to event.user.user_metadata[“hoo”] or app_metadata[“hoo”]
  3. In the following Action 2, check the value of event.user.user_metadata[“hoo”] or app_metadata[“hoo”] (using Realtime Webtask Log)

Cause

The event.user object in Action is a read-only snapshot of the user data created when an Action is executed.

Assigning a value to event.user.user_metadata[“hoo”] or app_metadata[“hoo”] will not update the source user data in the database.

Solution

Use api.users.setUsermetadata method.

(setAppMetadata for app_metadata)

//Action 1
api.users.setUserMetadata("hoo", "Updated Value")
//Action 2
console.log(event.user.user_metadata["hoo"]) // Outputs "Updated Value"