Encountered an unknown getter error in Vuex while using Vue/Nuxt/Vuex with server-side rendering enabled

I am encountering an error when I try to loop through the 'allPosts' data using a v-for directive on my div element.

Could it be possible that I have missed something related to the namespaced module transformation as mentioned in the Nuxt documentation about modules?

pages/index.vue

<template>
  <section id="postgrid">
    <div v-for="post in allPosts" :key="post.id"></div>
  </section>
</template>

<script>
import {mapGetters} from 'vuex'

import PostTile from '@/components/Blog/PostTile'

export default {
  components: {
    PostTile
  },
  computed: mapGetters(['allPosts'])
}
</script>

store/index.js

import Vue from 'vue'
import Vuex from 'vuex'

import Posts from './posts'

const store = new Vuex.Store({
  modules: {
    Posts
  }
})

store/posts.js

const state = () => ({
  posts: [
    {
      id: 0,
      title: 'A new beginning',
      previewText: 'This will be awesome don\'t miss it',
      category: 'Food',
      featured_image: 'http://getwallpapers.com/wallpaper/full/6/9/8/668959.jpg',
      slug: 'a-new-beginning',
      post_body: '<p>Post body here</p>',
      next_post_slug: 'a-second-beginning'
    },
    {
      id: 1,
      title: 'A second beginning',
      previewText: 'This will be awesome don\'t miss it',
      category: 'Venues',
      featured_image: 'https://images.wallpaperscraft.com/image/beautiful_scenery_mountains_lake_nature_93318_1920x1080.jpg',
      slug: 'a-second-beginning',
      post_body: '<p>Post body here</p>',
      prev_post_slug: 'a-new-beginning',
      next_post_slug: 'a-third-beginning'
    },
    {
      id: 2,
      title: 'A third beginning',
      previewText: 'This will be awesome don\'t miss it',
      category: 'Experiences',
      featured_image: 'http://eskipaper.com/images/beautiful-reflective-wallpaper-1.jpg',
      slug: 'a-third-beginning',
      post_body: '<p>Post body here</p>',
      prev_post_slug: 'a-second-beginning',
      next_post_slug: 'a-forth-beginning'
    }
  ]
})

const getters = {
  allPosts: (state) => state.posts
}

export default {
  state,
  getters
}

Answer №1

Your approach to setting up and accessing your store has a few issues that need addressing. To begin with, you are creating your store using the deprecated "classic mode," as mentioned in the documentation.

The classic mode feature will be removed in Nuxt 3.

To ensure you are following the latest practices, your store/index.js file should have a specific structure:

//store/index.js



//end

It is important to note that this file does not require any actual content; it just needs to exist. There is no need to import vue, vuex, or any other modules.

For your store/posts.js file, you can keep most of it unchanged. However, make sure to export constants for state, mutations, getters, and actions. Remove the final export statement at the bottom:

//store/posts.js
export const state = () => ({
  posts: [
    ...
  ]
})
export const mutations = {

}
export const actions = { 

}
export const getters = {
  allPosts: state => state.posts
}


//delete the following
export default {
  state,
  getters
}

Additionally, it appears that you may be using mapGetters incorrectly. After restructuring your store as suggested above, you can utilize it within pages/index.vue like this:

//pages.index.vue

<script>
import {mapGetters} from 'vuex'

export default {
  computed: {
    ...mapGetters ({
      allposts: 'posts/allPosts'
    })
  }
}
</script>

Once implemented correctly, you can access "allPosts" in your template like any other computed property, or refer to it as "this.allPosts" in your script.

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

Whenever I run "npm run build-dev" in Webpack with JavaScript, the browser continuously refreshes

I've been working on familiarizing myself with webpack lately. I've managed to convert everything to load modules and plugins, and it's all working fine when I use "npm run build-prod". Even when I use liveServer, the HTML loads properly. Ho ...

Retrieving the final selection made in vuetify v-select

Can someone please explain how to retrieve the last selected or removed value in a vuetify v-select when using the multiple property? For example, let's say we have a v-select with items [ 'Choice A', 'Choice B', 'Choice C&ap ...

What is the best way to utilize external CSS classes within a Vue component using @extend?

Suppose I have something like this in my component: <style lang="scss" scoped> .color-blue { color: blue; } .orange-blue { @extend .color-blue; // This is successful! @extend .bg-orange; // .bg-orange is defined elsewhere in a CSS fi ...

The 'xxx' type does not have an index signature, so the element is implicitly assigned an 'any' type

I'm currently facing an issue with TypeScript. The error message I'm encountering is related to the following section of code: The Interface: export default interface IUser { username: string; email?: string; isActive: boolean; group: s ...

Retrieving the maximum values from JSON data using D3

Currently, I am working with D3 and JSON data by using a function that looks like this: d3.json("http://api.json", function(jsondata) { var data = jsondata.map(function(d) { return d.number; }); After executing this, the value of the data becomes ["2", ...

Tips on altering button color when input field is populated

I have been researching online and have not come across any discussions on how to dynamically change the color of a button in JavaScript when an input field is filled. I haven't really tried any code myself because there are very limited resources add ...

The Axios POST request successfully sends the data to the Express server but unfortunately encounters a 404 error

Error 404 when Sending Data from Axios POST Request to Express Server Hey there! I'm currently working on setting up a user authentication server for a project, but I've hit a roadblock while attempting to send a POST request to my Node.js Expre ...

Generating option list from API JSON responses

I am currently developing a news app using React. I have set up my API to fetch data from newsapi.org and my goal is to display the available news source options in a dropdown list within my select component. However, instead of listing all news sources w ...

"Exploring the versatility of NextJS with dynamic route parameters

Can NextJS be configured to handle dynamic routes that match both /country and /country/city, while excluding matches like /country/city/whatever_content_here? The goal is to display the same content for either of the specified routes, regardless of whethe ...

Vue - unable to display component CSS classes in application when using class-style bindings

Just diving into Vue and starting with class-style binding syntax. I'm facing an issue where the CSS classes for header and footer that I've defined are not displaying, even though I've referenced them in the component tags. Can't seem ...

Is it possible to implement formvalidation.io in a React project that is using Materialize-css?

Can the formvalidation.io plugin be used with React and Materialize-css in a project? My project consists of multiple input components that may or may not be within a form. I want to utilize formvalidation for input validation. However, I am unable to find ...

hosting a NextJS development server on the local network

When launching ReactJS with the npm start command, the development server is opened on both localhost:3000 and the network at 192.168.1.2:3000. Testing the app on various devices was a breeze thanks to this setup. Now that I've delved into learning N ...

The issue with object alignment in the camera rotation is not properly centered in the Three.js framework

After bending two meshes into a circle to create a 3D marquee, I'm facing issues with centering them to the camera. For the full code, please visit https://jsfiddle.net/siiiick/1jh49e1u/ var text = "EXPRESS FREE SHIPPING WORLDWIDE OVER 200€ / ...

Revamp your code by utilizing ES6 class to replace multiple if statements in Javascript

Currently, my code is filled with numerous if statements and I am considering a switch to classes in ES6. However, Javascript isn't my strong suit... so PLEASE HELP ME..! Previous code: //example code function first(){ console.log('first sce ...

What is the best way to identify duplicate keys in fixed JavaScript objects?

My approach so far has been the following: try { var obj = {"name":"n","name":"v"}; console.log(obj); // outputs { name: 'v' } } catch (e) { console.log(e); // no exceptions printed } My goal is to detect duplicate keys within a ...

The process of incorporating a function into a website's source code using a web driver

In the source code, there is a button with an onclick event to change the paragraph content. However, the code provided does not seem to be functioning properly. ((JavascriptExecutor) this) .executeScript("function test123() { y=docume ...

Initial value in array remains unchanged

I have implemented a functionality where images work like checkboxes, but I am facing an issue where the first selected element does not receive the added class. <div v-for="tenant in shops" :key="tenant.id"> <div class="" style="position: rela ...

Issue with bookmarklet compatibility on mobile browsers like iOS and Android

Here is the bookmarklet code I have: javascript:(function(e,a,g,h,f,c,b,d){if(!(f=e.jQuery)||g>f.fn.jquery||h(f)){c=a.createElement("script");c.type="text/javascript";c.src="cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.js";c.onload=c.onreadystate ...

Finding the absolute root path of a npm package in Node.js

Objective I am on a quest to find a reliable method to ascertain the absolute root path of an npm package that has been installed in Node.js. Challenge Although I am aware of require.resolve, it only provides the entry point (path to the main module) an ...

Utilize the power of JavaScript or Lodash to substitute a string

Dealing with a scenario where a string contains multiple links that need to be replaced with actual HTML links, for example: <a href=”http://www.testing.com” target=_blank>http://www.asdfasd.com</a> Sample text: http://www.testing.com Sim ...