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

JSON.stringify(store) behaves inconsistently in the production and development environments #1149

Closed
PiNengShaoNian opened this issue Mar 14, 2022 · 3 comments

Comments

@PiNengShaoNian
Copy link

Reproduction

import { defineStore } from 'pinia'

const useCounterStore = defineStore('counter', {
  state: () => {
    return { }
  },
})

export default {
  setup() {
    const counter = useCounterStore()
     // The following line of code works fine in development mode but in production mode it throws an error that says Converting  
     //  circular structure to JSON
    //   --> starting at object with constructor 'Or'
    //  --- property '_renderProxy' closes the circle
    // at JSON.stringify (<anonymous>)
    JSON.stringify(counter)
  },
}

Steps to reproduce the behavior

  1. Pinia store as a parameter to JSON.stringify

Expected behavior

When pinia store is used as an argument to JSON.stringify, there should be consistent behavior in both production and development environments

Actual behavior

When pinia store is used as a JSON.stringify parameter, it will report an error in the production environment, but everything works fine in the development environment

Additional information

The pinia store in the production environment will have an additional "_p" attribute, resulting in circular references

@posva
Copy link
Member

posva commented Mar 14, 2022

A store isn't JSON-serializable. Only the state is (as long as you don't use circular references). I'm curious about why you were using JSON.stringify on the whole store though.

@posva posva closed this as completed Mar 14, 2022
@PiNengShaoNian
Copy link
Author

A store isn't JSON-serializable. Only the state is (as long as you don't use circular references). I'm curious about why you were using JSON.stringify on the whole store though.

My scenario is to use vue-query to query the data, he will automatically call the interface when the store changes, it is based on object serialization to serialize the store into a string to determine whether to perform a call or return a cached value

@posva
Copy link
Member

posva commented Mar 14, 2022

I see. You will either need to serialize the state only or use a serialization that handles circular references.

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

2 participants