2

When I set ES6 class to state on my vuex store in nuxt I got following warn:

 WARN  Cannot stringify arbitrary non-POJOs EndPoint

and When I use object as state is works without warning.

So How can I use ES6 classes in my state.

My model:

export default class EndPoint {
    constructor(newEndPoints) {
        this.login = newEndPoints.login;
        this.status = newEndPoints.status;
    }
}

and mutate state here:

commit(CoreMutations.SET_ENDPOINTS, new EndPoint(response.data));

Using object:

const EndPoint = {
    endPoints(newEndPoints) {
        return {
            login: newEndPoints.login,
            status: newEndPoints.status
        };
    }
};

and mutate:

commit(CoreMutations.SET_ENDPOINTS, EndPoint.endPoints(response.data));
BeHappy
  • 3,705
  • 5
  • 18
  • 59

1 Answers1

3

As discussion in comment add toJSON method in class solve the problem when setting state in client, but when set state in server, states will become undefined. so adding this code solve the problem:

    toJSON() {
        return Object.getOwnPropertyNames(this).reduce((a, b) => {
            a[b] = this[b];
            return a;
        }, {});
    }
BeHappy
  • 3,705
  • 5
  • 18
  • 59