Vuex bombards with errors without any clear explanation during commit

Within my Plugin, I am faced with the following code snippet:

import firebase from 'firebase'

export default context => {
  firebase.auth().onAuthStateChanged(userObject => {
    // eslint-disable-next-line no-console
    console.log({ userObject })
    context.store.commit('auth/setUser', { userObject })
  })
}

All seems well here as the userObject is accurately represented. Now, moving on to the store:

import Vue from 'vue'

export const state = () => ({
  user: null
})

export const mutations = {
  setUser(state, val) {
    // eslint-disable-next-line no-console
    console.log(val)
    Vue.set(state, 'user', val)
  }
}

Things start getting strange when this particular function triggers; my console becomes flooded with messages saying

Do not mutate vuex store state outside mutation handlers
. However, I am unable to pinpoint where exactly this improper mutation may be occurring. After hours of scouring through the code and attempting various adjustments unsuccessfully, I am reaching out for help in resolving this perplexing error. Thank you in advance.

Answer №1

The reason for the need to clone the userObject in the store when using Firebase is that Firebase itself can change the user object. To avoid this, create a deep clone of the userObject before setting it in the store:

export default context => {
  firebase.auth().onAuthStateChanged(userObject => {
    let clonedUserObject = JSON.parse(JSON.stringify(userObject))
    console.log({ clonedUserObject })
    context.store.commit('auth/setUser', { clonedUserObject })
  })
}

It seems like Vue.set is not being used correctly. The correct way to set an object is:

Vue.set(state.user, key, value)

However, you may not need to use Vue.set at all. You should be able to simply assign the value:

export const mutations = {
  setUser(state, val) {
    state.user = val
  }
}

If reactivity is an issue, initialize the user as an array and set it as follows:

export const state = () => ({
  user: []
})

export const mutations = {
  setUser(state, val) {
    console.log(val)
    Vue.set(state.user, 0, val)
  }
}

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

Encountering a 401 error when making an Axios request for data in a Laravel application

I have been working on fetching reports using axios in Laravel. Everything runs smoothly when tested with Postman, but I encountered error 401 here. Here is the code snippet: #Reports.vue file <template> <div> <h1>All reports ...

Animating avatars with Three.js

Seeking some guidance on utilizing the three.js library for creating animations. Specifically, I am interested in animating an avatar that has been exported from Blender to Collada format (.dae), to move an arm or a leg. I would like to achieve this anim ...

Vue parent component not receiving events properly

Referring to these sources: Forum Post Stack Overflow Question In my project, I am utilizing: CodeSandbox Example The setup involves the parent component listening for events emitted by a child component: mounted() { this.$on("edit-category& ...

What are some alternative ways to utilize jQuery siblings() without restricting it to the same parent element?

In the code provided below as Non-functional code [1], it will function correctly with siblings() to disable other input fields if their sum or amount exceeds a specified maximum value. This works effectively when all input fields are within the same paren ...

javascript The event handler is not functioning properly for the dynamically loaded AJAX content

I am facing an issue with adding a JavaScript event listener to a dynamically loaded div via AJAX. Below is my code snippet: var QuantityMiniCart = function() { var infor = document.querySelectorAll( '.mini-cart-product-infor' ); if ( ...

Initiating the Gmail React component for composing messages

Is it possible to use a React application to open mail.google.com and prefill the compose UI with data? This is a requirement that I need help with. ...

Can an image map be utilized within an <a> element?

I am attempting to implement an image map within an <a> tag. It seems to be functioning correctly in Chrome, but I am encountering issues with it not working in Internet Explorer. Is it acceptable to use an image map in an <a> tag? Below is th ...

Activate Pop-up for a single instance on BigCommerce

After researching and adding my own code, I am still struggling to get this question answered correctly. Here are the key points I am trying to achieve: 1. Automatically open a popup when the homepage loads. 2. Ensure that the popup is centered on all brow ...

Swapping out a class or method throughout an entire TypeScript project

Currently, I am working on a software project built with TypeScript. This project relies on several third-party libraries that are imported through the package.json file. One such library includes a utility class, utilized by other classes within the same ...

Having trouble getting my Sequelize model to export properly

For my school project, I am developing an e-commerce website and encountering an issue with connecting my GoogleStrategy to my SQLite database. The goal is to store user data in a table, but whenever I try to call the variable in my passport-setup file, an ...

ThreeJs is known for effortlessly handling an abundance of vertices, far surpassing the number typically found

I came across this code snippet: function loadObject(filePath){ var loader = new THREE.OBJLoader(); loader.load( filePath, function ( object ) { child = object.children[0]; var geometry = new THREE.Geometry().fromBufferGeometry( ch ...

Code timer for HTML redirection

Some time ago, I created this code to display random events on my website. While the code functions well, I now wish to incorporate a timer. I envision that when a user refreshes or enters the page, they will be redirected initially and then redirected ag ...

How to incorporate dynamic form elements into jquery.validate?

Even though I found a similar question on this link, the solutions provided there do not seem to work for me. I am currently using the query.validate plugin for my form, which works well in most cases. However, I have encountered an issue when adding new r ...

Issue with Submit Button Functionality following an Ajax Request

I am facing an issue where the submit button does not work after an ajax call, but works fine if I reload the page. The problem arises when a modal is displayed for email change confirmation. If the user confirms the change, the form submits successfully. ...

Calculate the total number of table rows added using jQuery

I am seeking help to identify the error in my code. My goal is to count the number of table rows added by the end user and display an alert box if the row count is not equal to 2. Below is my HTML code: <table width="100%" border="0" cellspacing="0" c ...

Applying unique textures to individual sides in Three.js

Here is the code for my textured cube: const textureLoader: TextureLoader = new TextureLoader(); const textureArray: MeshBasicMaterial[] = [ new MeshBasicMaterial({ map: textureLoader.load("./model/front.jpeg") }), new MeshBasicMaterial({ map ...

Error message: "Reactjs - TypeError: The property 'map' cannot be read as it is undefined in the table"

I have encountered an issue while using the material-ui table. I am able to map the items and display them successfully, but when trying to map the orders.items in the table, I get the following error: TypeError: Cannot read property 'map' of u ...

How can I send a file and a string request using the POST method to a Spring REST controller that accepts byte[] and Strings with Angular

Need help with sending a post method that includes a file and another string request parameter to a spring rest controller using angular. The server controller parameter is set up to receive an array of bytes for the file and another string request wrappe ...

Capturing authorization issues in Firebase using ngrx effects

I am currently working on an angular5/firebase/ngrx project and I'm encountering an issue when trying to handle errors during the signup process. When I attempt to submit the signup form with an email that already exists in the system, I receive the ...

Learn how to dynamically switch the background image of a webpage using a button in AngularJS

Hey there, I'm currently working on incorporating interactive buttons into my website to give users the ability to customize the background. I've been experimenting with Angular to achieve this feature. So far, I've managed to change the ba ...