mobx-state-tree icon indicating copy to clipboard operation
mobx-state-tree copied to clipboard

Get Error: [mobx-state-tree] Cannot modify 'AnonymousModel@..., the object is protected and can only be modified by using an action.

Open geohuz opened this issue 4 years ago • 8 comments

Bug report

  • [x ] I've checked documentation and searched for existing issues
  • [x] I've made sure my project is based on the latest MST version

I've created a newly project by following the document, I found the

applySnapshot(plist, 
 {
    persons: {
      "1": {
          id: 1,
          name: "abc"
     }
}

throw error Error: [mobx-state-tree] Cannot modify 'AnonymousModel@/persons/1(id: 1)', the object is protected and can only be modified by using an action.

I can only fix it by adding unprotect(plist), if the protected behavior is the default, this should be documented not to confuse the new comer.

Describe the observed behavior

geohuz avatar Jan 17 '21 15:01 geohuz

Could you please create a small codesandbox.io example?

On Sun, Jan 17, 2021, 9:08 AM geohuz [email protected] wrote:

Bug report

  • [x ] I've checked documentation and searched for existing issues
  • I've made sure my project is based on the latest MST version

I've created a newly project by following the document, I found the

applySnapshot(plist, { persons: { "1": { id: 1, name: "abc" } }

throw error Error: [mobx-state-tree] Cannot modify 'AnonymousModel@/persons/1(id: 1)', the object is protected and can only be modified by using an action.

I can only fix it by adding unprotect(plist), if the protected behavior is the default, this should be documented not to confuse the new comer.

Describe the observed behavior

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/mobxjs/mobx-state-tree/issues/1647, or unsubscribe https://github.com/notifications/unsubscribe-auth/AABCW4VLLC2C7R4FFSGCI6TS2L4ONANCNFSM4WGFRJFA .

mattruby avatar Jan 17 '21 16:01 mattruby

It looks like this is expected behaviour. It's also mentioned in the docs for types.array, so might also be applicable here.

https://mobx-state-tree.js.org/API/#array

Here's a repro: https://codesandbox.io/s/mst-1647-c9g0z?file=/src/index.js

willrax avatar Jan 18 '21 09:01 willrax

Thanks @willrax , but the example in frontpage should mention this constraint, otherwise one takes hour to figure out why it gets failed.

geohuz avatar Jan 19 '21 08:01 geohuz

Thanks @willrax , but the example in frontpage should mention this constraint, otherwise one takes hour to figure out why it gets failed.

Agreed.

willrax avatar Jan 19 '21 09:01 willrax

@geohuz We'll update the docs. I was just thinking about it though and I think if you were to apply a snapshot, doing this through an action would mean you don't have to mark the tree as unprotected. I've updated the codesandbox. I think this approach fits a little more with "MST way". Thoughts?

https://codesandbox.io/s/mst-1647-c9g0z?file=/src/index.js

willrax avatar Jan 19 '21 20:01 willrax

@willrax I agreed with you for putting the applySnapshot into action would make sense for the protection purpose. BTW, I would like to remind you that the following code in the front page also fails for the same reason:

const twitterStore = TwitterStore.create({ tweets: [ { body: "Anyone tried MST?" } ] })

in this case I have to const twitterStore = TwitterStore.create() first then unprotect(twitterStore), I don't know if there is elegant way to do it.

geohuz avatar Jan 20 '21 05:01 geohuz

Interesting. Lots to fix up on this page! Quick fixes, ill open a pr

willrax avatar Jan 20 '21 22:01 willrax

What was the error you were getting on the twitter example? I rebuilt it here and it seemed to be ok:

https://codesandbox.io/s/confident-shape-eph07?file=/src/App.js

willrax avatar Jan 20 '21 22:01 willrax