Error when use Promise .then(): Cannot modify '...', the object is protected and can only be modified by using an action. #2005
-
I have array inside model:
I'm always getting error I tried to rewrite the code from forEach to map:
and got the same error. Then I tried to add the action addAPIItem to Destination
but I couldn't find a way to call it from the action fetchDestinations. If I don't use Promise .then no errors occur (but the code is bad, don't use it):
I'm stuck with Promise .then usage. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
So I think when you chain off a promise with That pulls the code execution outside of the async generator you're using, and MST doesn't allow changing state outside actions You said your last code block is bad, but I think that's the correct thing to do here. Assuming Can you say more about what's bad about your last block which yields the response value and uses it afterward? Or perhaps send a CodeSandbox to illustrate additional problems you're having? |
Beta Was this translation helpful? Give feedback.
-
I've modified action code by your suggestions and it works!
Types checking works and I didn't receive any error message :) |
Beta Was this translation helpful? Give feedback.
So I think when you chain off a promise with
.then()
, you turn the.then()
clause into effectively an async function call.That pulls the code execution outside of the async generator you're using, and MST doesn't allow changing state outside actions
You said your last code block is bad, but I think that's the correct thing to do here. Assuming
apiFetchDestinations
is some async function that returns a response (which would have beenvalue
in your.then()
method), it should work.Can you say more about what's bad about your last block which yields the response value and uses it afterward? Or perhaps send a CodeSandbox to illustrate additional problems you're having?