Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Rayray's avatar

state.cartitems.push is not a function at Store.setusercart

Am using Vue Js and am trying to add products to cart using Localstorage so that latter I can check them out. My code looks okay but am geting that error. Below is the code

			state : {
   							 cartitems: [],
			},

			getters : {
    						cartitems:(state )=> state.cartitems,
			},

			mutations : { 
    					setusercart :(state,usercartitem ) => (state.cartitems.push(usercartitem)), 
			 },

			actions : {
							async addstocktocart( { commit }, data) {
        						commit('seterror', '')
        						commit('setmsg'data. Titlele + 'has been added to Cart')
        						commit('setloader', 'addstocktocart')
        						commit('setusercart', data)
        						console.log(this.state.auth.usercartitems)	
    						},
				}
0 likes
8 replies
vincent15000's avatar

Do you see cartitems in the Vue Devtools in the browser ?

vincent15000's avatar

@Rayray This line is strange.

commit('setmsg'data. Titlele + 'has been added to Cart')

What are you trying to do with that ?

'setmsg'data. Titlele
Rayray's avatar

@vincent15000 Its just for a popup notification that the item has been added to cart. Am trying to make sure the user can add many items to cart them process them all at once. so i want every time each product is added to cart its saved in the local storage

	commit('setmsg' , data.title + 'has been added to Cart')
1 like
vincent15000's avatar

@Rayray Ok but now your code is better written, there was some bad typo in your previous comment.

Rayray's avatar

Yes, it's the same issue.

1 like

Please or to participate in this conversation.