Getting user profile custom field via JS

Hi,

I’m wondering if it is possible to retrieve user profile custom fields in our customized header using something like this:

$.ajax("/session/current").then(function (json) { 
    console.log(json.custom.user_field_4);
}

What would be the correct syntax?
Thanks!

1 个赞

You want something like

$.ajax("/session/current").then(function(result) {
  console.log(result.current_user.custom_fields);
});
2 个赞

Thanks, how do I get to the attribute?
console.log(result.current_user.custom_fields); yields an object,
i also tried:
result.current_user.custom_fields.my_field_name
and
result.current_user.custom_fields.user_field_4
they all return “undefined”

2 个赞

You will need to whitelist the custom fields you want using the “public_user_custom_fields” site setting.

3 个赞

got it thanks! :smile:

3 个赞

Is this still accurate? I’ve added pro_expires_at as a custom field on my user (User.find('nuck').tap { |u| u.custom_fields['pro_expires_at'] = 5.weeks.from_now.to_i }.save) and stuffed that into public_user_custom_fields in the site settings panel, but I’m just not seeing it in the preload data?

2 个赞

It definitely should still be working. We haven’t removed it and some plugins rely on it to work.

What preload info are you looking at? Viewing a user’s profile?

currentUser and the custom_user_fields attribute that the PostSerializer says should be present on serialized Post objects

It should definitely work if it’s public_user_custom_fields – I know we’re using this feature on some plugins and the data is indeed being displayed properly. Maybe try restarting your dev server? Does it appear in the JSON request?

2 个赞

If you want a custom_user_field on the post object you can do this:

https://github.com/xfalcox/discourse-signatures/blob/1ff4e7e1d57eed825febdbd58980a73bef03cb6b/plugin.rb#L19

1 个赞

public_user_custom_fields in the admin panel, right? Not some setting elsewhere?

I was actually developing against our staging server which rebuilds the docker every time so I’m certain it was restarted (and then some). I’ll look at the JSON request this weekend and see if I can pin this down.

Wouldn’t this cause an N+1 query? Is there any solid documentation on the whole custom field system somewhere? I didn’t realize you could register the custom field type or append to serialized_current_user_fields

I don’t think so, because the serializer already has the user object, is just a matter of including it in the payload. I can be wrong, tough.

Nope, but there are plenty of opensource plugins and the Discourse source code itself.

I found it trough other plugins code too.

Right, public_user_custom_fields in the admin panel. Those fields should end up in the JSON request. I’d double check there. Without a development environment your life is going to be a lot harder, as the next step would be to go into the code and add Rails.logger.info to try and figure out why those fields are not being passed through when they should.

1 个赞

出于某种原因,我在控制台中看不到我在“管理员”>“自定义”>“用户字段”部分添加的自定义用户字段。

我在插件中添加了以下内容:

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

当我查看 user 对象时,我在 custom_fields 条目下看到一个空对象。当我将其更改为 console.log(user.custom_fields); 时,它仍然显示一个空对象。

当前用户有三个已填写的自定义字段,我已将这三个字段的键(格式为 test_topic)添加到“公共用户自定义字段”和“管理员用户自定义字段”中,但对象在控制台中仍然显示为空。

我问了 ChatGPT(哈哈),它似乎没有帮助我。

有什么关于可能发生的情况的建议吗?

更新:我认为一个问题是我使用了 custom_fields,而现在它似乎在 user_fields 标签下,如:

但是,它似乎只向我显示自定义用户字段的值和一个数字作为键,而不是我在“管理员”>“自定义”>“用户字段”中输入的自定义键。

1 个赞

这个主题组件可能与您想对用户字段进行的操作有关。

这个组件添加了根据用户监视的词语来隐藏帖子的功能。

查看源代码可能会给您带来一些想法。

1 个赞

我主要想创建一个小的自定义组件,以确保我在管理后台 > 自定义 > 用户字段中添加的自定义用户字段:

  1. 添加到可翻译的 i18n 中,以便我可以在管理后台 > 自定义 > 文本部分自定义它们的文本
  2. 添加为插值键,以便我可以在管理后台 > 自定义 > 文本部分将它们用作 %{key}

基本上是想解决我在这里发布的我自己的问题:
https://meta.discourse.org/t/create-and-configure-custom-user-fields/113192/55

但我无法通过仅从用户对象中获取键/值组合的第一部分。

我会看看你添加的内容,谢谢!

1 个赞

我还是个新手,边做边学。我不能确定,但如果涉及到语言翻译,有两个插件可以进行翻译。

以及

如果我误解了你的目标,我深表歉意。

1 个赞