I am experiencing issues with local storage getItem() function not functioning properly within NUXT JS

Currently working on creating a shopping cart using nuxt js. Successfully able to store the cart data in local storage, but facing difficulty in retrieving the data. Refer to the screenshots for more details: https://i.sstatic.net/X7dL9.png https://i.sstatic.net/Lk6QR.png

Note: I prefer not to use vuex-persistedstate.

Answer №1

I have identified several minor issues with this code.

Firstly, it appears that you have included both addToCart and saveInLocalStorage, particularly evident by your execution of saveInLocalStorage.

Mutations should strictly be utilized to modify the state of a single Vuex variable.

They should adhere to the principle of purity, meaning they should not trigger any side effects.

It is essential to implement an action for saveInLocalStorage, and a subsequent action for executing the commit on the addToCart mutation prior to utilizing dispatch to initiate the saveInLocalStorage action.

Furthermore, it is unclear where you are actually calling the function responsible for retrieving data from local storage. Is it executed at the top level of the Vuex store?

If so, in order to rectify the issue related to the Vuex store not being updated, it is recommended to have the function responsible for fetching the value of storedCart and then applying it to set cart as a mutation to ensure the Vuex store is properly updated.

Lastly, utilizing a ternary operation in this scenario is unnecessary. It is more straightforward to directly assign an empty array to the Vuex cart object.

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

How can I transfer request headers from Express to index.js in React?

Is there a way to store user-related information received in the request headers of the Express server as a runtime variable accessible in index.js? I need to apply conditional routing based on these parameters. Alternatively, is there a way to pass these ...

Ways to delete a property from an element that was originally included with jquery

On my jsp page, I am implementing a jQuery autocomplete feature for a text field with the id "mytextfield". jQuery(function(){ $("#mytextfield").autocomplete("popuppages/listall.jsp"); }); Sometimes, based on user input and pr ...

Tips for managing open and closed components within a React accordion and ensuring only the clicked component is opened

Unique Accordion component: const CustomAccordion = (props: AccordionProps) => { const { label, levels, activeId, id } = props const [isExpand, setIsExpand] = useState(false) const onPress = useEvent(() => { setIsExpand( ...

How to return the same value as the input value in Vue before the action is completed

When creating a component and module for functionality X while using Vuex for state management, the code initially works fine. However, after the first insertion, the Getter function consistently returns the previous input value before the action is commit ...

The image data is not displaying in the $_FILES variable

I am having an issue updating an image in my database. I have a modal that loads using jQuery. When I click on the save modification button, all the form data appears except for the image file, which does not show up in the $_FILES array in PHP. I have tri ...

Incorporating Dynamic Javascript: A Step-by-Step Guide

I came across a select object that looks like this: <select id="mySelect" onchange = "start()" > <option>Apple</option> <option>Pear</option> <option>Banana</option> <option>Orange</option> < ...

A step-by-step guide on recompiling Vue using shell_exec()

I am facing an issue with my Vue application that is supposed to receive updates via webhook.php. In this setup, there is a PHP script responsible for updating internal configurations. The problem arises at the end of the webhook.php file where I have incl ...

Different color for background of division

I am working with a structure that looks like this: <div class="wrapper"...> <a href="#"...>blah</a> <div class="post"...>stuff</div> </div> This structure repeats multiple times on a dynamic page. I am looking f ...

Displaying the URL of a link on the screen using jQuery

I am looking for a solution to add a link address after the link itself in a table containing reference links for our staff. I have used CSS to achieve this in the past, but the address was not copyable by users. Instead, I am interested in using jQuery&ap ...

When the Angular script is executed, the variable is not defined

One of my custom directives receives an object named 'vm' in its scope, which contains a property/value pair (ccirCategoryIncidentI : 3) that I need to access. When I try to log this value with console.log(scope.vm.ccirCategoryIncidentI), it init ...

Unlocking JSON data with identical keys using JavaScriptLearn how to access JSON data with

I need help converting my JSON training data that includes classes and sentences into a different format. [ { class: 'Reservation', sentence: 'make reservation' }, { class: 'Reservation', sentence: 'do reservation&a ...

I am unable to pass the req.params.id value as an input to a function located in a separate file

In my project, I've developed a REST API for user and coupon management. The main file driving this API is called coupon-api.js. This file contains the route definitions, while the functions to handle these routes are separated into two distinct files ...

Ways to prevent a <a href> element with a class linked to javascript from behaving like a block element

I am currently facing an issue with formatting an inline navigation. The last link, which is associated with a JavaScript class, is causing the entire link to become a block element instead of aligning inline with the rest of the links in the navigation. ...

.mouseleave() triggers when exiting a designated boundary area

I'm attempting to implement a roll-up feature for a div when the mouse is over another div. The issue I'm facing is that the roll-up div closes even if the mouse just exits through the bottom border. Is there a way to achieve this using JavaScrip ...

This route does not allow the use of the POST method. Only the GET and HEAD methods are supported. This limitation is specific to Laravel

I am encountering an issue while attempting to submit an image via Ajax, receiving the following error message: The POST method is not supported for this route. Supported methods: GET, HEAD. Here is the Javascript code: $("form[name='submitProfi ...

Angular-UI keypress event allows users to interact with components and

I am facing challenges while using the keypress feature in the following code snippet- <div ng-controller="GBNController"> ... <input id="search-field" type="text" placeholder="JSON Query" ng-model="queryText" ui-keypress="{enter: foo()}"/> .. ...

Tips for making jQuery maphilight function properly?

Can someone assist me with Mapilight? I have been trying to get it to work but no success so far. Here are the script links in the head section of my HTML. <script type="text/javascript" src="/js/jquery.js"></script> <script type="text/ja ...

What could be causing the sluggish performance of my wysiwyg editor in Vue.js?

I've been exploring the use of a wysiwyg editor on my webpage. Implementing simple two-way data binding with v-model="text" and straightforwardly outputting <div v-html="text"></div>. As a beginner in vuejs, I have encountered a performanc ...

Is there a way for multiple <select> elements to have identical options in React?

Currently, I have a React component structured like this: export default function ExampleComponent() { return ( <div> <select required name="select1"> <option label=" "></opti ...

Seeking assistance for my dissertation assignment: electron, express, and SQLite3

I am currently dedicated to my thesis project, which involves designing an educational desktop video game for both public and private schools within my city. Being a fan of electron, I thought it would be a great idea to develop my app using this platform ...