Getting user profile custom field via JS

For some reason, I can’t see the custom user fields in the console that I added in the Admin > Customize > User Fields section.

I’ve put the following in a plugin:

<script type="text/discourse-plugin" version="0.8.42">
   const user = api.getCurrentUser();
   console.log(user);
</script>

When I look in the user object, I see a blank object under the custom_fields entry. When I change it to console.log(user.custom_fields); it still shows a blank object.

The current user has three custom fields filled out, I’ve added all three of those field keys, in the format of test_topic to the public user custom fields and staff user custom fields and yet the object still appears empty in the console.

I’ve asked ChatGPT (lol) and it doesn’t seem to be helping me.

Any suggestions on what could be happening?

UPDATE: I think one problem was that I was using custom_fields and now it seems to be under the user_fields label, as per this:

However, it seems to just show me the value of the custom user field and a number for the key, instead of the custom key I entered in Admin > Customize > User Fields.

1 Like