Upon refreshing the page, the Vuex store getter displays [__ob__: Observer]

After each page refresh, my Vuex store getter returns [__ob__: Observer]. The version of Vue being used is 2.2.3 along with TypeScript.

Initially, the data loads correctly from an external API, but when utilizing the getter, it fails and requires another call to the API to retrieve the data for the page.

The console log displays the following after the first page load:

[{…}, {…}, {…}, {…}, __ob__: Observer]

There are 4 objects that show up correctly on the page. However, I am puzzled as to why an Observer is appended at the end.

Since the getter works fine on the initial load, I can confirm that the setter responsible for adding the data to the store post API retrieval is functioning properly.

When the page is refreshed, the same console log only showcases the Observer:

[__ob__: Observer]

This is how the getter in my store is structured:

get getItems() {
  console.log('Store items -> ', this.items) // shows empty array upon refreshing the page
  var parsedObj = JSON.parse(JSON.stringify(this.items)) 
  return parsedObj
}

Despite attempting the solution suggested in this particular question, the issue persists.

The component linked to the store contains a simple getter call, which functions without issues during the initial page load but falters upon refresh:

async created() {
  const result = await myModule.getItems
  console.log('[dev page] items from store -> ', result) // appears empty upon refresh
  this.fmData = result 
  console.log('fm in Created -> ', this.fmData) // only reveals 'Observer' upon refresh
}

Although Pinia may present a more suitable alternative for managing state today, I am currently restricted to using Vuex for this project.

I am uncertain about the steps to take in order to resolve this issue.

Answer №1

When utilizing any store (whether Vuex or Pinia), persistence is not included as a default feature. Vue is not meant to handle those aspects anyway. Luckily, there are numerous extensions available for both stores that can provide persistence functionality.

Feel free to check out my earlier response for additional links and information.

Answer №2

After spending some time, I realized that setting the value in the store requires using actions to call mutations.

mutations: {
    setUserId(state, id) {      
        state.activeUserId = id;
    }
},
actions: {
    updateUserId: ({commit, state}, id) => {
        commit('setUserId', id)
        return state.activeUserId;
    }
}

Initially relying solely on mutations caused issues, but implementing actions resolved them and allowed me to access this.$store.state.activeUserId from any view.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Stop automatic scrolling when the keyboard is visible in Angular

I have created a survey form where the user's name appears on top in mobile view. However, I am facing an issue with auto scroll when the keyboard pops up. I want to disable this feature to improve the user experience. <input (click)="onFocusI ...

Ways to turn off v-tooltip when a user clicks outside of it

I am using v-tooltip to display tooltips on each cell of a table <td v-tooltip="{content: 'some content', trigger: 'click'}"></td> However, I would like the current tooltip to disappear when the user clicks on another cell ...

Getting the name of a component in Vue using webpack vue-loader

Recently, I have been immersing myself in the Vue documentation and learning about components. However, there is one thing that is confusing me. The documentation mentions different ways to register a component: Global Registration Vue.component(' ...

Using Typescript and React to manage controlled components and selecting a single checkbox within a group

all. I am currently working on a controlled component in Storybook using React and Typescript. When my Checkbox is uncontrolled, it works perfectly fine. However, I am facing some challenges with the logic and thought process when transitioning it to a ...

Inquiries regarding vue-router and the addition of routes

Main javascript file: import Vue from 'vue' import App from './App.vue' import router, { initLayout } from './router' import store from './store' require('../mock/index') Vue.config.productionTip = false ...

Any ideas on how to fix the issue of 'type' property being absent in 'AsyncThunkAction' with Redux Toolkit while working with TypeScript?

While working with Redux Toolkit and the thunk/slice below, I decided to handle errors locally instead of setting them in state. To achieve this, I followed the example provided here. Although I could have set an error in the state, I wanted to explore wh ...

Minimize the count of switch cases that are not empty

How can I optimize the number of cases in my switch statement to align with SonarQube recommendations? Currently, I have 37 cases in a switch statement, but SonarQube recommends only 30. I believe that my code is functioning correctly, and the issue lies ...

Guide on setting up a route in Next.js

Recently, I developed a simple feature that enables users to switch between languages on a webpage by adding the language code directly after the URL - i18n-next. Here's a snippet of how it functions: const [languages, ] = React.useState([{ langua ...

Data merging in Firebase 9 and Vue 3 is not functioning properly

I am facing an issue with merging data in my firebase database. I consulted the documentation at https://firebase.google.com/docs/firestore/manage-data/add-data for guidance. After attempting to merge data using setDoc, I encountered an error (Uncaught Ty ...

Deactivate specific choices from a dynamically generated dropdown menu in Angular 8

I am working on a dynamic dropdown feature with multiple fields. https://i.sstatic.net/28iQJ.png By pressing the + button, a new row is generated. Users can add any number of rows. My challenge is to prevent users from selecting previously chosen values i ...

Building vue js logic to manage state across multiple ul li elements

I have a situation where I have an unordered list with a static number of list items (menu elements) that are rendered on the server side. I need to add some state-based logic to each list item, such as changing the color of the active item and expanding ...

Tips on how to retrieve a variable from an array in Vue JS

New to Javascript and Vue, I am exploring examples to grasp the fundamental concepts. <template> /*<p v-bind:class="['bold', 'italic', isValid ? 'valid' : 'invalid']">*/ <p v-bind:class= ...

Tips for utilizing the 'crypto' module within Angular2?

After running the command for module installation: npm install --save crypto I attempted to import it into my component like this: import { createHmac } from "crypto"; However, I encountered the following error: ERROR in -------------- (4,28): Canno ...

"Revolutionizing the way we navigate: Angular's innovative

Presently, my focus is on incorporating route transitions into my project. I've employed a component that appears on click and triggers the corresponding service function: routeTransition(destination) { if (this.router.url !== destination) { t ...

Obtain an instance tuple from tuple classes using TypeScript 3.0 generic rest tuples type

When it comes to retrieving the correct instance type from a class type, the process typically involves using the following code: type Constructor<T = {}> = new (...args: any[]) => T class Foo {} function getInstanceFromClass<T>(Klass: Co ...

In a Custom Next.js App component, React props do not cascade down

I recently developed a custom next.js App component as a class with the purpose of overriding the componentDidMount function to initialize Google Analytics. class MyApp extends App { async componentDidMount(): Promise<void> { await initia ...

How do Angular, React, and Vue address the primary issue between JavaScript and the DOM?

I am curious about the major issues that arise between JavaScript and DOM, which frameworks like Angular, React, and Vue attempt to address. I am a beginner learning Angular and would appreciate any guidance on this topic. https://i.sstatic.net/nxFDn.png ...

Angular HttpClient Catch Return Value

In my quest to develop a universal service for retrieving settings from the server, I've encountered an issue. When errors arise, I want to intercept them and provide a default value (I have a predetermined configuration that should be utilized when e ...

Having trouble with assigning an error message in Formik validation using TypeScript

Currently, I am in the process of constructing a user input form in React & TypeScript using Formik. The form requires the user to input a name, email, and password. The input handling is functioning properly, but now I aim to implement validation functio ...

Prettier seems to be producing varied outcomes across various machines

My teammate and I are collaborating on the same project, but we're facing an issue where our Prettier configurations conflict. Each time we push our code to Github, his Prettier format overrides mine. Here's an example of his formatting: const in ...