My custom data does not work

Hello,

I’m trying to set up customData with App Service and Authentication but it doesn’t work.

I am currently on a shared mongodb, I activated in Authentication>User Setting the custom data as follows:

I have my function which is close to that of the example in the documentation :

exports = async function onUserCreation(user) {
  const customUserDataCollection = context.services
    .get("mongodb-atlas")
    .db("kiizmi-dev")
    .collection("custom_users_data");
  try {
    await customUserDataCollection.insertOne({
      // Save the user's account ID to your configured user_id_field
      userId: user.id,
      // Store any other user data you want
      test: user.id,
      test2: 1,
      role: 'default'
    });
  } catch (e) {
    console.error(`Failed to create custom user data document for user:${user.id}`);
    throw e
  }
}

And which is configured with authentication system and in private.

But when I create a user (email/password) through the interface, the function doesn’t seem to run and I don’t see an insert in the collection.

When I connect with the user, his custom data is null even if I enter manually.

Thanks to anyone who takes the time to help me.

Hi @Kiizweb_Kiizmi,

I have my function which is close to that of the example in the documentation

May or may not be important, but what if you remove the explicit function name?

exports = async function(user) {
  …
};

But when I create a user (email/password) through the interface

Do you mean the Portal UI? What if you create the user in your app?

the function doesn’t seem to run and I don’t see an insert in the collection.

Have you tried to add more console.log(…) and see if you get anything in the logs?

When I connect with the user, his custom data is null

Can you also please post the code you use in the client?

even if I enter manually.

When you insert it manually, do you ensure that userId is in string format? ObjectId won’t work there.

Thanks a lot for your help.

The problem was that the function only executes when the user is created via the application and not directly on the UI.

And for reading, userId must be of type string. Thanks again

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.