To run User Creation Function when user created with Appservice UI

I am trying to run funciton once user is created .I am creating user with Appservices UI and in user settings enabled Custom User Data and wrote function there which is below one for testing purpose but my custom_user collection is not updating even after user created .

import { v4 as uuidv4 } from 'uuid';
exports = async function (user) {
  const customUserDataCollection = context.services
    .get("mongodb-atlas")
    .db("myapp")
    .collection("custom_user");
  try {
    await customUserDataCollection.insertOne({
      // Save the user's account ID to your configured user_id_field
      user_id: user.id,
      name: "Swaminathan"
    });
    return customUserDataCollection.find({user_id:user.id});
  } catch (e) {
    console.error(`Failed to create custom user data document for user:${user.id}`);
    throw e
  }
} 

Hello,

You can take a look at triggers.

Create an AUTHENTICATION type trigger and link the trigger to your function. The function should take in authEvent as a param.

In your case:

exports = async function (authEvent) {
  const customUserDataCollection = context.services
    .get("mongodb-atlas")
    .db("myapp")
    .collection("custom_user");
  try {
    return await customUserDataCollection.insertOne({
      // Save the user's account ID to your configured user_id_field
      user_id: authEvent.user.id,
      name: authEvent.user.data.name // your user metadata field
    });
  } catch (e) {
    console.error(`Failed to create custom user data document for user:${user.id}`);
    throw e
  }
}