# Client-side: capturing data from "/posts" server-side response?

**URL:** https://meta.discourse.org/t/client-side-capturing-data-from-posts-server-side-response/82209
**Category:** Development
**Created:** [March 5, 2018, 1:22pm UTC](https://meta.discourse.org/t/client-side-capturing-data-from-posts-server-side-response/82209 "2018-03-05T13:22:20Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![ChrisBeach](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/chrisbeach/32/214628_2.png) [@ChrisBeach](https://meta.discourse.org/u/ChrisBeach)
#### Post date: [March 5, 2018, 1:22pm UTC](https://meta.discourse.org/t/client-side-capturing-data-from-posts-server-side-response/82209/1 "2018-03-05T13:22:20Z")

</div>

I have included a field in the server-side response returned when a user creates a new topic. I’d like to update the client-side with this data.

The field is called `stripe_txn_balance`, and is present in the following response from the POST to `/posts`:

> **JSON response**
>
> ```json
> {
> "action": "create_post",
> "post": {
> "id": 30,
> "name": null,
> "username": "chris",
> "avatar_template": "/letter_avatar_proxy/v2/letter/c/958977/{size}.png",
> "created_at": "2018-03-05T13:09:45.534Z",
> "cooked": "\u003cp\u003easdasdasdasdasdasdasdasdad\u003c/p\u003e",
> "post_number": 1,
> "post_type": 1,
> "updated_at": "2018-03-05T13:09:45.534Z",
> "reply_count": 0,
> "reply_to_post_number": null,
> "quote_count": 0,
> "avg_time": null,
> "incoming_link_count": 0,
> "reads": 0,
> "score": 0,
> "yours": true,
> "topic_id": 24,
> "topic_slug": "dasdasd",
> "display_username": null,
> "primary_group_name": null,
> "primary_group_flair_url": null,
> "primary_group_flair_bg_color": null,
> "primary_group_flair_color": null,
> "version": 1,
> "can_edit": true,
> "can_delete": false,
> "can_recover": null,
> "can_wiki": true,
> "user_title": null,
> "actions_summary": [
> {
> "id": 5,
> "hidden": true,
> "can_act": true
> },
> {
> "id": 3,
> "can_act": true
> },
> {
> "id": 4,
> "can_act": true
> },
> {
> "id": 8,
> "can_act": true
> },
> {
> "id": 7,
> "can_act": true
> }
> ],
> "moderator": false,
> "admin": true,
> "staff": true,
> "user_id": 1,
> "draft_sequence": 1,
> "hidden": false,
> "hidden_reason_id": null,
> "trust_level": 4,
> "deleted_at": null,
> "user_deleted": false,
> "edit_reason": null,
> "can_view_edit_history": true,
> "wiki": false,
> "stripe_txn_balance": 0
> },
> "success": true
> }
> 
> ```

When I get the response I’ll be doing something like this on the client side:

```javascript
Discourse.User.current().set("stripe_txn_balance", post.stripe_txn_balance);

```

What I’d like to know is where’s best to put this code?

I tried doing the following in an initializer but couldn’t find my field in the `model`:

```plaintext
api.modifyClass('controller:composer', {
    save(force) {
        const promise = this._super(force);
        const model = this;
        if (promise) {
            promise.then(function(value) {
                console.log(model);
            })
        }
    }
});

```

`model.topic` evaluates to this, but it doesn’t include my field (and I also couldn’t find my field elsewhere within `model`:

 ![30](https://global.discourse-cdn.com/meta/original/3X/9/e/9e3c0d791d0d23eee8912e2574a0112f9fef908f.png)

A nudge in the right direction would be much appreciated.

---

<div class="post-metadata">

### Author: ![vinothkannans](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/vinothkannans/32/86465_2.png) [@vinothkannans](https://meta.discourse.org/u/vinothkannans)
#### Post date: [March 5, 2018, 3:53pm UTC](https://meta.discourse.org/t/client-side-capturing-data-from-posts-server-side-response/82209/2 "2018-03-05T15:53:12Z")

</div>

I think message bus is more suitable for your purpose.

---

<div class="post-metadata">

### Author: ![ChrisBeach](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/chrisbeach/32/214628_2.png) [@ChrisBeach](https://meta.discourse.org/u/ChrisBeach)
#### Post date: [March 5, 2018, 4:01pm UTC](https://meta.discourse.org/t/client-side-capturing-data-from-posts-server-side-response/82209/3 "2018-03-05T16:01:41Z")

</div>

Message bus would be good in order to decouple the code.

However, I’m still none the wiser as to how to extract the stripe\_txn\_balance field from the composer post response from the server?

---

<div class="post-metadata">

### Author: ![vinothkannans](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/vinothkannans/32/86465_2.png) [@vinothkannans](https://meta.discourse.org/u/vinothkannans)
#### Post date: [March 5, 2018, 4:19pm UTC](https://meta.discourse.org/t/client-side-capturing-data-from-posts-server-side-response/82209/4 "2018-03-05T16:19:48Z")

</div>

The JSON response is loaded inside [`result.responseJson`](https://github.com/discourse/discourse/blob/master/app/assets/javascripts/discourse/controllers/composer.js.es6#L510). Try like below

```javascript
promise.then(result => {
  console.log(result.responseJson);
})

```

---

<div class="post-metadata">

### Author: ![ChrisBeach](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/chrisbeach/32/214628_2.png) [@ChrisBeach](https://meta.discourse.org/u/ChrisBeach)
#### Post date: [March 5, 2018, 10:06pm UTC](https://meta.discourse.org/t/client-side-capturing-data-from-posts-server-side-response/82209/5 "2018-03-05T22:06:34Z")

</div>

Thanks @vinothkannans for your suggestion.

After much fiddling this is the only approach that worked:

```javascript
api.modifyClass('model:composer', {
    createPost(opts) {
        const result = this._super(opts);
        if (result) {
            result.then(r => {
                if (r.payload && typeof(r.payload.stripe_txn_balance) !== 'undefined') {
                    Discourse.User.current().set("stripe_txn_balance", r.payload.stripe_txn_balance);
                }
            });
        }
        return result;
    }
});

```
