# 用户卡片帖子名称：reply/edit 后 userModel.custom\_fields 不起作用

**URL:** <https://meta.discourse.org/t/user-card-post-names-usermodel-custom-fields-not-working-after-reply-edit/360541>\
**Category:** Development\
**Created:** [2025年四月5日 16:00 UTC](https://meta.discourse.org/t/user-card-post-names-usermodel-custom-fields-not-working-after-reply-edit/360541 "2025-04-05T16:00:15Z")\
**Posts on this page:** 1\
**Showing post:** 1

<div class="post-metadata">

**Author:** ![LeoDavidson](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/leodavidson/32/119574_2.png) [@LeoDavidson](https://meta.discourse.org/u/LeoDavidson)\
**Post date:** [2025年四月5日 16:00 UTC](https://meta.discourse.org/t/user-card-post-names-usermodel-custom-fields-not-working-after-reply-edit/360541/1 "2025-04-05T16:00:15Z")

</div>

我认为这可能是 Discourse 的一个 bug，但不确定。也许是我自己操作不当。

我有一个插件，它向 `user-card-post-names` 连接器添加信息。

### Handlebars：

> <https://github.com/LeoDavidson/discourse-directoryopus/blob/master/assets/javascripts/discourse/templates/connectors/user-card-post-names/directoryopus-link-status-card.hbs>

### Javascript：

> <https://github.com/LeoDavidson/discourse-directoryopus/blob/master/assets/javascripts/discourse/templates/connectors/user-card-post-names/directoryopus-link-status-card.js.es6>

这又会调用下面的 Javascript 来完成大部分工作：

> <https://github.com/LeoDavidson/discourse-directoryopus/blob/master/assets/javascripts/lib/directoryopus-link-lib.js.es6>

### 问题详情：

在前面的 JS 代码中，有这两行：

```javascript
var version = userModel.custom_fields && userModel.custom_fields["directoryopus_link_version"];
var edition = userModel.custom_fields && userModel.custom_fields["directoryopus_link_edition"];

```

然后它会进行一个测试 `if (version || edition) { ... }` 并根据结果生成一些字符串（`opusLinkClass`、`opusLinkIcon`、`opusLinkText`），然后将其存储：

```javascript
component.set("opusLinkClass", opusLinkClass);
component.set("opusLinkIcon", opusLinkIcon);
component.set("opusLinkText", opusLinkText);

```

这些字符串随后在本文开头的 handlebars 文件中使用。

浏览器日志窗口中从未出现任何错误或警告，但有时会出现问题。

我正在使用一个同时设置了 `custom_fields` 的帐户进行测试，因此 `if (version || edition)` 测试应该始终进入第一个分支，而不是下面的 `else if (!viewingStaff)`。

但有时它会进入第二个分支，这意味着 `version` 和 `edition` 未被设置。

这通常不会发生，这就是为什么我认为这可能是 Discourse 的一个 bug。

这似乎只会在我创建新帖子然后回复它，或者编辑我刚做的回复时才会发生。这就像在某些情况下，由于某个地方的竞态条件，连接器运行时 `userModel` 对象没有正确初始化？

这似乎只会在每个帖子里发生 _一次_。如果我刷新页面，一切都会恢复正常，并且在该帖子里会一直正常。 （至少据我所知。它似乎是半随机的，所以有可能我还没有在同一个帖子里再次看到它发生。）

感谢您的时间和任何建议。

---

_[View the full topic](https://meta.discourse.org/t/user-card-post-names-usermodel-custom-fields-not-working-after-reply-edit/360541)._
