Issue with displaying Vue data from vuex in the user interface

I'm facing a challenge in displaying plan data on my Vue application. The data is fetched from an API running locally. Although I have successfully added the data to the store/vuex and verified its correctness using vue dev tools, I am unable to visualize it on the user interface. I need assistance in troubleshooting why the data isn't appearing on the UI.

Within my plans.vue file, I simply utilize a for loop over the plans data, which works when I manually input the data into the array. Therefore, there seems to be an issue between the store and the data prop causing the problem.

store.js

state: {
 plans: []
}
mutations: {
  loadPlans(state, plans) {
     state.plans = plans
 }
},
getters: {
    loadPlans(state) {
      return state.plans
    },
}

plans.vue

created() {
        PageOptions.pageEmpty = true;
        this.$store.dispatch('loadPlans')
    },
    data() {
        return {
            plans: this.$store.getters.loadPlans,
        }
    },

This code snippet has been simplified to focus solely on the issue at hand; there is additional unrelated code present as well.

Answer №1

Consider using a similar approach:

datastore.js

state: {
  data: []
},
getters: {
  retrieveData(state) {
    return state.data
  },
},
mutations: {
  setData(state, newData) {
    state.data = newData
  }
},
actions: {
  async fetchData({commit}) {
    await this.$axios.get('http://localhost/api/data', function(response) {
      commit('setData', response.data)
    })
  }
}

info.vue

async beforeCreate() {
  await this.$store.dispatch('fetchData')
},
computed: {
  myInfo() {
    return this.$store.getters.retrieveData
  }
}

Answer №2

If you want to retrieve the value using a computed method, consider that the data object is evaluated prior to setting the state value. Adjust your code accordingly by making the following changes:

data:{
    return {};
}
,computed: {
    plans: this.$store.getters.loadPlans
}

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

Generating a tree structure using a JavaScript array

Looking to build a tree structure from a given list of data where the paths are represented like: A-->B-->C-->D-->E.. A-->B-->C-->D-->F.. A-->F-->C-->D-->E.. . . . All possible data paths are stored in an array. The de ...

Alternative method to jQuery's "find" selector

$('.wrapper a').filter('a'); //returns all anchors I am trying to find a way to select all anchor elements using a specific selector. The issue is that the find method only looks at descendants, so I need an alternative solution. Any s ...

Issue with the navbar toggler not displaying the list items

When the screen is minimized, the toggle button appears. However, clicking it does not reveal the contents of the navbar on a small screen. I have tried loading jQuery before the bootstrap JS file as suggested by many, but it still doesn't work. Can s ...

Retrieves MySQL SQL findings and converts them into a JSON structure

I'm in the process of integrating Typeahead.js into my website. The Typeahead.js plugin will retrieve data from a remote page that returns JSON, similar to: http://example.org/search?q=%QUERY Here's the PHP code I've written for my site: ...

Sorting two different divisions is an example

I need advice on how to toggle between two divs, A and B, without having to reload the page. Ideally, I would like to have three buttons - one that shows only div A when clicked, another that displays only div B, and a third button that shows both A and ...

Error: It is not possible to add the "providers" property as the object is not extendable within N

Ever since updating to the latest version of NGRX, I've been encountering an issue: users$= createEffect(() => this.actions$ .pipe( ofType(users.LOAD_USERS), tap((action: users.LoadUsersAction) => { action.item.name = ...

Preserve the JavaScript Promise for later utilization

While experimenting with Node.js to construct the server side RESTApi, I encountered a situation where it functioned effectively during my independent tests. However, once deployed in a live environment, issues related to overflow arose. Specifically, when ...

A JQuery function linked to the click event of the body element triggers another click event right away

$().ready(function(){ $("#move-to").click(function(){ $("body").bind("click",function(){ alert("foo"); }); }); }); Why do I receive an alert message of "foo" immediately after clicking on "move-to"? When I bind the " ...

A guide on implementing listings in React Native through the use of loops

I am trying to display the data retrieved from an API, but I am encountering an error. // Retrieving the data. componentWillMount() { tokenner() .then(responseJson => { const token = "Bearer " + responseJson.result.token; ...

What causes Node's crypto module to generate varying outputs for identical strings?

I'm currently attempting to execute the following program: var crypto = require('crypto'); var a = crypto.createHash('md5').update('89Zr-J591').digest('hex'); var name = '89Zr−J591'; var b = crypto. ...

Alter the font color upon clicking the menu using jQuery

When I click on the menu and sub-menu items, I want to change their colors along with their parent. You can see an example of how I want it to work here. However, currently, when I click on a sub-menu item, the color of the parent menu item gets removed. ...

Running the command "npm run dev" generates a series of files named 0.js, 1.js, all the way up to 14.js within the

As a novice in the realm of Webpack, NPM, and VueJS, I find myself facing a perplexing issue. Despite my efforts to seek answers online, I remain stumped by the outcome when executing the command npm run dev in VueJS - wherein webpack generates 15 files l ...

Can we store multiple Foreign Keys in Laravel in order to display numerous items from different tables in a Package Items table within Quasar 1 Laravel 2 framework?

My goal is to create an Ecommerce website where each product can belong to multiple categories. I have a form called PackageItem table (similar to PRODUCT) and when I select an item, it retrieves the name from the Item table (similar to CATEGORIES). http ...

The Image component in a test within Next.js was not wrapped in an act(...) during an update

After setting up a basic NextJS app with create-next-app and integrating Jest for testing, I encountered an error message stating "An update to Image inside a test was not wrapped in act(...)". The issue seems to be related to the Image component in NextJS ...

Issue with Angular $compile directive failing to update DOM element

I'm currently working on a project that involves integrating AngularJS and D3 to create an application where users can draw, drag, and resize shapes. I've been trying to use angular binding to update the attributes and avoid manual DOM updates, b ...

How can scriptlets be used to dynamically create a properly functioning model?

Having trouble populating data into the modal form. I've tried placing the modal code inside a for-each loop, and while it successfully displays the details in the modal, the functionality of the close button and clicking outside the modal to close it ...

Javascript's ReferenceError occasionally acts inconsistently when using Firefox's scratchpad

While delving into the world of Javascript for learning purposes, I encountered an unexpected behavior. Let's explore this scenario: function hello(name) { let greet = 'Hello ' alert(greet + name) } hello('world') alert(gree ...

NPM rimraf - proceed with the execution even if directories are not found (do not halt with exit code 1)

My npm script is set up to run a series of synchronous commands, starting with npm run clean:install". Here is the sequence: "install:all": "npm install && bower install", "clean": "npm run rimraf -- node_modules doc typings coverage wwwroot bow ...

Assistance with Redirecting Responses in Next.js API Routes

I'm currently working on implementing a login functionality in Next.js. I have made significant progress but I am facing an issue with redirecting to the homepage after a successful login. My setup involves using Next.js with Sanity as the headless CM ...

Refreshing the page to dynamically alter background and text hues

I'm currently dealing with a website that generates a random background color for a div every time it is refreshed. I have a code that successfully accomplishes this: var colorList = ['#FFFFFF', '#000000', '#298ACC', &ap ...