# api.modifyClass sometimes(!) not working

**URL:** https://meta.discourse.org/t/api-modifyclass-sometimes-not-working/212413
**Category:** Bug
**Created:** [December 17, 2021, 7:54am UTC](https://meta.discourse.org/t/api-modifyclass-sometimes-not-working/212413 "2021-12-17T07:54:25Z")
**Posts on this page:** 1
**Showing post:** 12

<div class="post-metadata">

### Author: ![RGJ](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/rgj/32/523185_2.png) [@RGJ](https://meta.discourse.org/u/RGJ)
#### Post date: [December 20, 2021, 5:54pm UTC](https://meta.discourse.org/t/api-modifyclass-sometimes-not-working/212413/12 "2021-12-20T17:54:20Z")

</div>

Reclassifying this as bug.

I have added a `console.log` in the plugin API code `app/assets/javascripts/discourse/app/lib/plugin-api.js` so it logs whenever `modifyClass` is being called.

I have removed all external plugins to make sure there was not a conflict somewhere.

Repro:

- create an empty forum on `stable` (so no Ember CLI). This is not working on tests-passed (without Ember CLI) either. I did not test this with Ember CLI.

- add a theme component with this in Common - Head

### #1 Working

```plaintext
<script type="text/discourse-plugin" version="0.1">
    api.modifyClass('model:user', {
      pluginId: 'test-tc',
      testFunction: function() {
        return 1;
      } 
    });
</script>

```

- load the home page

- console shows `modifyClass called for model:user _application-08d9058ddd37ba80992f770509f4919ad0738a17f14fb85167b1dc1f32f8b56e.js:23490:16 Object { pluginId: "test-tc", testFunction: testFunction() }`

- enter `Discourse.currentUser.testFunction()` in console

- “1” is printed

### #2 Failing

- Go to a topic, for instance ‘Welcome to Discourse’ and reload the page
- console shows the same “modifyClass called” logs
- enter `Discourse.currentUser.testFunction()` in console
- `Uncaught TypeError: Discourse.currentUser.testFunction is not a function` is printed

### #3 Failing with warning

- Append a single line to top of the theme component so it looks like this:

```plaintext
<script type="text/discourse-plugin" version="0.1">
    const userModel = api.container.lookup("model:user");

    api.modifyClass('model:user', {
      pluginId: 'test-tc',
      testFunction: function() {
        return 1;
      } 
    });

</script>

```

- Go to a topic, for instance ‘Welcome to Discourse’ and reload the page
- console shows the same “modifyClass called” logs
- console shows a warning `"model:user" was already cached in the container. Changes won't be applied.`
- enter `Discourse.currentUser.testFunction()` in console
- `Uncaught TypeError: Discourse.currentUser.testFunction is not a function` is printed

### #4 Working

- Move the lookup line to **bottom** of the theme component so it looks like this:

```plaintext
<script type="text/discourse-plugin" version="0.1">
    api.modifyClass('model:user', {
      pluginId: 'test-tc',
      testFunction: function() {
        return 1;
      } 
    });

    const userModel = api.container.lookup("model:user");
</script>

```

- Go to a topic, for instance ‘Welcome to Discourse’ and reload the page
- console shows the same “modifyClass called” logs
- enter `Discourse.currentUser.testFunction()` in console
- “1” is printed 🥳

---

_[View the full topic](https://meta.discourse.org/t/api-modifyclass-sometimes-not-working/212413)._
