# Transaction not beeing applied

**URL:** <https://discuss.prosemirror.net/t/transaction-not-beeing-applied/5706>\
**Category:** Uncategorized\
**Created:** [July 19, 2023, 1:10pm UTC](https://discuss.prosemirror.net/t/transaction-not-beeing-applied/5706 "2023-07-19T13:10:58Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![bpospanov](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/bpospanov/32/3516_2.png) [@bpospanov](https://discuss.prosemirror.net/u/bpospanov)\
**Post date:** [July 19, 2023, 1:10pm UTC](https://discuss.prosemirror.net/t/transaction-not-beeing-applied/5706/1 "2023-07-19T13:10:58Z")

</div>

I have two realization with different approach but with almost the same code inside. but checkAnswer is working, checkAnswer2 is not

they are iterating through children of node and changes attributes of nodes particular type

```
checkAnswer2() {
  const from = this.getPos();
  const to = from + this.node.nodeSize;
  this.editor.state.doc.nodesBetween(from, to, (node) => {
    if (node.type.name === this.extension.options.inputNodeName) {
      let classes = '';
      if (node.attrs.value) {
        classes = node.attrs.answers.includes(node.attrs.value) ? 'success' : 'error';
      }
      const position = node.attrs.position;
      // const position = pos;
      node.attrs.classes = classes;
      const newAttrs = { ...node.attrs, classes: classes };
      this.editor.view.state.tr.setNodeMarkup(position, undefined, newAttrs);
    }
  });
},
checkAnswer(node) {
  if (!node?.content?.content?.length) {
    if (node.type.name === this.extension.options.inputNodeName) {
      this.checkFillGapsInput(node);
    }
  }

  // проверяем потомков рекурсивно
  if (node?.content?.content?.length) {
    node.content.content.forEach((node) => this.checkAnswer(node));
  }
},
checkFillGapsInput(node) {
  let classes = '';
  if (node.attrs.value) {
    classes = node.attrs.answers.includes(node.attrs.value) ? 'success' : 'error';
  }
  const position = node.attrs.position;
  node.attrs.classes = classes;
  this.editor.view.state.tr.setNodeMarkup(position, undefined, node.attrs);
},

```

first one runs all the children recursively and checks if its the node type we need, second uses nodesBetween where it takes getPos() as a begin and getPos() + nodeSize as the end

---

<div class="post-metadata">

**Author:** ![marijn](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/marijn/32/15_2.png) [@marijn](https://discuss.prosemirror.net/u/marijn)\
**Post date:** [July 19, 2023, 7:48pm UTC](https://discuss.prosemirror.net/t/transaction-not-beeing-applied/5706/2 "2023-07-19T19:48:18Z")

</div>

> [@bpospanov](#):
>
> ```auto
> this.editor.view.state.tr.setNodeMarkup(position, undefined, newAttrs);
> 
> ```

A transaction only has an effect when you dispatch it. In this line, you are creating one but then doing nothing with the object.

---

<div class="post-metadata">

**Author:** ![bpospanov](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/bpospanov/32/3516_2.png) [@bpospanov](https://discuss.prosemirror.net/u/bpospanov)\
**Post date:** [July 20, 2023, 5:50am UTC](https://discuss.prosemirror.net/t/transaction-not-beeing-applied/5706/3 "2023-07-20T05:50:23Z")

</div>

Thanks for reply, Marijn!

Now I tried

`this.editor.view.dispatch(this.editor.view.state.tr.setNodeMarkup(position, undefined, newAttrs));`

also

`this.editor.dispatchTransaction(this.editor.view.state.tr.setNodeMarkup(position, undefined, newAttrs));`

but it did not work

But even if the reason is the transaction not beeing dispatched why it worked in first function where were no dispatch at all?

---

<div class="post-metadata">

**Author:** ![bpospanov](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/bpospanov/32/3516_2.png) [@bpospanov](https://discuss.prosemirror.net/u/bpospanov)\
**Post date:** [July 20, 2023, 6:09am UTC](https://discuss.prosemirror.net/t/transaction-not-beeing-applied/5706/4 "2023-07-20T06:09:13Z")

</div>

I noticed that in first function window.MutationObserver is triggered to rerender DOM, but in the second function editor state is kinda beeing updated but its not triggering browser DOM to update

---

<div class="post-metadata">

**Author:** ![bpospanov](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/bpospanov/32/3516_2.png) [@bpospanov](https://discuss.prosemirror.net/u/bpospanov)\
**Post date:** [July 20, 2023, 9:53am UTC](https://discuss.prosemirror.net/t/transaction-not-beeing-applied/5706/5 "2023-07-20T09:53:54Z")

</div>

![Запись экрана 2023-07-20 в 15.50.28](https://discuss.prosemirror.net/uploads/secondsite/original/2X/4/4c796d61413606786a702b5d7db2543504c3fdc7.gif)

here you can see that it does not checks the spelling at first time but when I trigger update data by new input it updates the class attribute and then get highlighted with green

second input the same, at first I filled the wrong answer, but right after the Check button it did not highlight red color, only after triggering state

---

<div class="post-metadata">

**Author:** ![marijn](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/marijn/32/15_2.png) [@marijn](https://discuss.prosemirror.net/u/marijn)\
**Post date:** [July 20, 2023, 4:19pm UTC](https://discuss.prosemirror.net/t/transaction-not-beeing-applied/5706/6 "2023-07-20T16:19:15Z")

</div>

> [@bpospanov](#):
>
> But even if the reason is the transaction not beeing dispatched why it worked in first function where were no dispatch at all?

That definitely did nothing as well. Maybe your conclusion that it did was mistaken.

Also, you appear to be mutating data you shouldn’t mutate (`node.attrs.classes`) which could add the the confusion.

---

<div class="post-metadata">

**Author:** ![bpospanov](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/bpospanov/32/3516_2.png) [@bpospanov](https://discuss.prosemirror.net/u/bpospanov)\
**Post date:** [July 21, 2023, 2:44am UTC](https://discuss.prosemirror.net/t/transaction-not-beeing-applied/5706/7 "2023-07-21T02:44:45Z")

</div>

1. may be, but how is it possible that the classes were applied
2. mutating the classes is the idea of the parent node, It has the “check answers” button, it iterates through child nodes of particular type and checks if the value of input is allowed

---

<div class="post-metadata">

**Author:** ![bpospanov](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/bpospanov/32/3516_2.png) [@bpospanov](https://discuss.prosemirror.net/u/bpospanov)\
**Post date:** [July 21, 2023, 7:04am UTC](https://discuss.prosemirror.net/t/transaction-not-beeing-applied/5706/8 "2023-07-21T07:04:45Z")

</div>

Coul you please explain the idea why I shouldnt chang node’s inner attributes from other one?

---

<div class="post-metadata">

**Author:** ![marijn](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/marijn/32/15_2.png) [@marijn](https://discuss.prosemirror.net/u/marijn)\
**Post date:** [July 21, 2023, 7:28pm UTC](https://discuss.prosemirror.net/t/transaction-not-beeing-applied/5706/9 "2023-07-21T19:28:36Z")

</div>

If you try to use this library without reading the docs, you’re going to have a terrible time. For this specific issue, see [this section](https://prosemirror.net/docs/guide/#doc.identity_and_persistence).

---

<div class="post-metadata">

**Author:** ![bpospanov](https://discuss.prosemirror.net/user_avatar/discuss.prosemirror.net/bpospanov/32/3516_2.png) [@bpospanov](https://discuss.prosemirror.net/u/bpospanov)\
**Post date:** [July 24, 2023, 11:28am UTC](https://discuss.prosemirror.net/t/transaction-not-beeing-applied/5706/10 "2023-07-24T11:28:39Z")

</div>

Thanks for reply, Marijn!

I understand that I shouldn’t mutate node.attrs properties directly. But changing them via transaction.setNodeAttribute or transaction.setNodeMarkup is okay, right? Just don’t forget to dispatch it.

If suggestion above is correct is it okay to update attributes of child nodes with setNodeAttribute method from parent node?
