Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error: TypeError: 'ownKeys' on proxy: trap result did not include 'scope' #162

Open
xmlking opened this issue Apr 8, 2019 · 18 comments
Open

Comments

@xmlking
Copy link

xmlking commented Apr 8, 2019

after switching to @ngxs-labs/immer-adapter": "^2.0.1", i am getting following error

  @Mutation()
  @Action(CreateNewConversation)
  createConversation(ctx: StateContext<ChatBoxStateModel>) {
    const newConversation = new Conversation('payload.conversationId'); 
    ctx.setState((state: ChatBoxStateModel) => {
      state.conversations.push(newConversation);
      state.activeConversationId = newConversation.id;
      return state;
    });
  }
core.js:5567 ERROR TypeError: 'ownKeys' on proxy: trap result did not include 'scope'
    at Function.freeze (<anonymous>)
    at deepFreeze (ngxs-store.js:1514)
    at ngxs-store.js:1529
    at Array.forEach (<anonymous>)
    at deepFreeze (ngxs-store.js:1519)
    at Object.setState (ngxs-store.js:1607)
    at setStateValue (ngxs-store.js:1728)
    at Object.setState (ngxs-store.js:1785)
    at Object.setState (ngxs-labs-immer-adapter.js:82)
    at ChatBoxState.push.../../libs/chat-box/src/lib/state/chat-box.store.ts.ChatBoxState.createConversation (chat-box.store.ts:192)
@splincode
Copy link
Member

You need disabled development mode

@splincode
Copy link
Member

However, this is a bug and I need to reproduce it.

@xmlking
Copy link
Author

xmlking commented Apr 8, 2019

@splincode
Copy link
Member

@xmlking well I was able to reproduce the error

image

@splincode
Copy link
Member

Fixed

image

@xmlking
Copy link
Author

xmlking commented Apr 9, 2019

I cannot import
import { ImmutableContext, ImmutableSelector } from '@ngxs-labs/immer-adapter';
some how they are missing in published v3.0.0 npm module!

image

@splincode
Copy link
Member

Sorry

@splincode splincode reopened this Apr 9, 2019
@splincode
Copy link
Member

splincode commented Apr 9, 2019

done (v3.0.1)

@xmlking
Copy link
Author

xmlking commented Apr 9, 2019

No problem. appreciate your work and quick release 👍

@victos
Copy link

victos commented Jul 31, 2019

Again

ERROR TypeError: 'ownKeys' on proxy: trap result did not include 'scope'
at Function.freeze ()
at deepFreeze (ngxs-store.js:1420)
at ngxs-store.js:1435
at Array.forEach ()
at deepFreeze (ngxs-store.js:1425)
at ngxs-store.js:1435
at Array.forEach ()
at deepFreeze (ngxs-store.js:1425)
at Object.setState (ngxs-store.js:1649)
at setStateValue (ngxs-store.js:1770)

@splincode
Copy link
Member

@victos create issue with reproduce

@victos
Copy link

victos commented Jul 31, 2019

This error occurred when I get the property from getState and put it back within setState. Maybe this is not a bug. The property get from the getState is not a pure JavaScript object.

@splincode
Copy link
Member

I will not be able to help unless I see specific examples.

@victos
Copy link

victos commented Jul 31, 2019

@splincode splincode reopened this Aug 1, 2019
@dfa1234
Copy link

dfa1234 commented Aug 14, 2019

Sorry to emphase but It's a catastrophic bug.
I'm losing a lot of time to make a workaround for this.
Any advise would be welcome, thank.

@victos
Copy link

victos commented Aug 14, 2019

Sorry to emphase but It's a catastrophic bug.
I'm losing a lot of time to make a workaround for this.
Any advise would be welcome, thank.

Maybe you can use the function isDraft to know if an object is a Proxy, and use function original to get the pure JavaScript object from the Proxy
Here’s the doc of immer:
https://github.com/immerjs/immer#extracting-the-original-object-from-a-proxied-instance

@dfa1234
Copy link

dfa1234 commented Aug 14, 2019

Thank. I learned I need to learn more about Proxy :)

Anyway I finally found a workaround, before reading you comment.
I presume original from immer is better than my solution, which is simply to make a deep clone (using JSON parse/ stringify)

  @Action(OpenCategory)
  @ImmutableContext()
  openCategory({setState, getState}: StateContext<CatalogState>, {categoryToOpen}: OpenCategory) {

    let state: CatalogState = cloneDeep(getState());

    state.current.page = 0;
    state.current.product = null;

   // until now, we are cool.`setState` would work:
   // setState(s => state);

   // But now this line would error my setState:
   // state.current.category = categoryToOpen;

   // `categoryToOpen` is indeed a proxy
   // So I simply had to make a clone to make is work (cloneDeep is just a JSON parse /stringify)
    state.current.category = cloneDeep(categoryToOpen);

   // Edit: just tested and indeed, using original work like  charm
   state.current.category = original(categoryToOpen);
   
   //working
   setState(s => state);

}

Any comment on the above code is more than welcome

@xtreemrage
Copy link

xtreemrage commented Nov 5, 2019

@dfa1234
I would not use JSON serialization if you have a complex object with functions etc. You you can loose properties, like Function and Infinity and you will also lose properties with undefined values.

Only use it if you have a simple object with only values.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants