Having trouble retrieving information from Vuex store in Vue component when using TypeScript

I'm facing an issue with fetching data in my Vue component using Typescript. Upon logging in, I trigger an API call to retrieve data. Once the data is received, I store it using a Vuex module.

@Action async getData(): Promise<TODO> {
  return new Promise<TODO>((resolve, reject) => { 
    getData("getData").then(res => {
       if(res) {
         console.log('Result -> ', res) // Data is here
         this.items = res // save response in a module variable
         resolve()
       }
      ...
     })

When I switch to another Vue page, I invoke the following function from the same store module within created(). I made it async/await thinking that was causing the problem.

 async created() {
    const storeData = await classesModule.getItems 
    this.myData = storeData
    console.log('Data in Created -> ', this.myData) // No data found
 }

Here's the getter function in the store:

get getItems() {
    console.log('GET THE ITEMS object -> ', this.items) // No data found  
    return this.items
  }

Both console.log statements above show the same result in the console, which looks like this:

[__ob__: Ot]
length: 0
__ob__: Ot {value: Array(0), dep: _t, vmCount: 0}
[[Prototype]]: Array

The array is empty, indicating there's no data present.

If anyone can assist me with this issue, I would greatly appreciate it.

Answer №1

getters: {

  fetchItems(state) {
    console.log('FETCH THE ITEMS object -> ', state.items) 
    return state.items
  }
}
state:{
     items:[]
}

You can retrieve the 'items' object using the first parameter of getters, which is 'state'.

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

What is the process for activating the quasar timepicker once a user has selected a time?

The functionality of the timepicker in Quasar doesn't quite meet my expectations. I don't want to add another library just for this feature. The main issue I have is that it doesn't close automatically after selecting a time. I managed to fi ...

Having trouble setting default value using formControl in a component shared across multiple forms

Currently, I am facing an issue with setting a default value in a custom dropdown by using formControlName. In order to attach formControls to a shared component, I am utilizing ControlValueAccessors in the shared component. This allows me to update the fo ...

Encountering an error with TypeScript generic parameters: Expecting '?' but receiving an error message

As a newcomer to TypeScript, I am currently exploring the use of generic type parameters. I have encountered an error message: '?' expected while working on a function and a type that both require type arguments. The issue seems to be in the Inpu ...

Having trouble with integrating jsPlumb with Nuxtjs and encountering the error "document is not defined"?

I've seen some similar questions, but none of the solutions have worked for me so here's my issue. I'm attempting to integrate jsPlumb into my Nuxt.js/Vue.js application. Here are the steps I've taken: npm i jsplumb --save Create a ne ...

Having trouble resolving errors in Visual Studio Code after failing to properly close a parent function? Learn how to fix this issue

Here we have the code starting with the construct function followed by the parents function public construct() //child { super.construct; } protected construct() //parent { } The issue arises where I am not receiving an er ...

Issue with displaying entire object using Jest and console.dir

I'm having trouble displaying an error in my Jest test because it's not showing all the levels as expected. import util from 'util' describe('Module', () => { it('should display all levels WITHOUT util', () =& ...

What is the best way to execute a method in a component for each iteration in an *ngFor loop in Angular 2

Is there a way to call a method for each iteration of *ngFor and pass the iterated variable as a parameter? For example: <li *ngFor="let element of componentModel | keys">{{element.key}}--{{element.value}}</li> Then, in the component, I have ...

Arrange the items that are missing from Array B to be located at the bottom of Array A, organized in a file tree structure

I have two arrays containing different types of objects. Each object in the arrays has a title assigned to it. My goal is to compare these two arrays based on their titles and move any files that are not included in the bottom part of the fileStructure arr ...

Managing a promise and using async/await functions

Having an issue with my node and express function router.post('/', async (req, res) => { const playlist = new Playlist({ song: req.body.song, artist: req.body.artist }) try { const newPlaylist = await play ...

Interface definition triggers an error when assigning a string literal to a field

When defining a string literal type in an interface, I encountered unexpected behaviors. interface IFoo { value: 'foo' | 'boo'; } Upon implementation of the interface in a class, I encountered an error: class Foo implements IFoo ...

Error message: TypeScript type mismatch in Material DateTimePicker onChange function

Presented below is my implementation of a React + Material DatePicker, following the example outlined in the official documentation. let App: React.FC = () => { const [dateStart, handleDateStart] = useState(new Date()); const [dateEnd, handleDateEnd] ...

The use of custom loaders alongside ts-node allows for more flexibility

Is it possible to utilize ts-node with a custom loader? The documentation only mentions enabling esm compatibility. ts-node --esm my-file.ts I am attempting to implement a custom loader for testing an ESM module, but I prefer not to rely on node for compi ...

What are the steps to fixing the "Parameter is not compatible with the argument" issue that occurs while working with CASL and Prisma in TypeScript?

I recently came across this helpful resource while trying to implement CASL with Prisma. However, I encountered an error in my code. import { user } from '@prisma/client'; import { PureAbility, AbilityBuilder, subject } from '@casl/ability&a ...

Issues with Vue js not updating the DOM

I am currently working with multiple components: A, B, and C. In component A, there is an array that plays a crucial role in the flow of data. When an item is selected in component A: An emit event triggers a method in component B that displays the lengt ...

Rendering illuminated component with continuous asynchronous updates

My task involves displaying a list of items using lit components. Each item in the list consists of a known name and an asynchronously fetched value. Situation Overview: A generic component named simple-list is required to render any pairs of name and va ...

Changing the Express.Request.user type from optional User to required User for authorized routes: A guide

Currently, I am developing a server using Express and Typescript. I have integrated passport js for authenticating the routes I have set up. However, one issue that I encounter is that Express.Request.user is defined as Express.User | undefined. This means ...

Exploring the structure and dispersion of files within a VueJS project: An architectural inquiry

As I delve into learning VueJS, I've come to understand that .vue files typically consist of three distinct parts: <template>, <script>, and <style>. My query pertains to how these three parts are managed in a professional VueJs pro ...

Secure a reliable result from a function utilizing switch statements in Typescript

There's a function in my code that takes an argument with three possible values and returns a corresponding value based on this argument. Essentially, it can return one of three different values. To achieve this, I've implemented a switch statem ...

Upon migrating from Vue CLI 2 to 3, an error is thrown stating: "Cannot locate element: #app" and an empty body is

Currently, I am in the process of transitioning my VueJS project from VueCLI 2 to version 3. After moving all the necessary files to the src folder, I attempted to view it in the browser by running npm run serve. However, I encountered a problem where the ...

Seems like ngAfterViewInit isn't functioning properly, could it be an error on my end

After implementing my ngAfterViewInit function, I noticed that it is not behaving as expected. I have a hunch that something important may be missing in my code. ngOnInit() { this.dataService.getUsers().subscribe((users) => {this.users = users) ; ...