Removing a specific value from a Pinia reactive data store

Currently, I am developing a multi-step registration form that includes an option for users to upload an avatar. As it's a complex form, I've decided to store the data in a Pinia store until the final submission. Everything seems to be functioning correctly so far. However, I'm facing an issue regarding the ability to delete the Blob URL value associated with the avatar, allowing users to select a different image. My attempted solution was to set the avatar value to '' using

userRegisterStore.cardOwner.avatar = ''
, considering its initial state is an empty string. Unfortunately, this approach resulted in an error message:

runtime-core.esm-bundler.js:218 Uncaught TypeError: 'set' on proxy: trap returned falsish for property 'avatar'

In addition, I make use of cropperjs and vue-cropperjs libraries, although these may not be directly related to the issue at hand. After spending hours searching online without success, I'm hoping someone here can provide some assistance.

[EDIT] I have created a sample demonstration on codesandbox.io. You can access RegisterFormFive.vue either through the provided link or by using the integrated preview in codesandbox: . Upload an image, crop it (orange button below the image), and try deleting it (red button).

Below is a snippet of my code:

// RegisterDataStore.js

export const useRegisterDataStore = defineStore('RegisterDataStore', {
  state: () => ({
    imgReady: false,
    cardOwner: reactive({
      firstName: '',
      lastName: '',
      email: '',
      password: '',
      agbAccepted: false,
      dsgvoAccepted: false,
      title: '',
      companyName: '',
      companyPublic: false,
      position: '',
      positionPublic: false,
      avatar: '',
      addresses: [],
      contacts: [],
      links: [],
    }),
  }),
// Cropper part

<Cropper
      v-if="registerDataStore.cardOwner.avatar && !registerDataStore.imgReady"
      class="mx-auto max-h-[350px] max-w-[350px] overflow-hidden rounded-lg border-2 border-skin-primary bg-skin-primary"
      ref="cropper"
      alt="User avatar"
      drag-mode="move"
      :src="registerDataStore.cardOwner.avatar"
      // Other Cropper configurations...
    ></Cropper>
// More snippets follow...

Answer №1

Avoid unnecessary nesting of reactive in the Pinia state, as it is already reactive by default. Creating a @click.prevent handler directly in the template may not impact functionality but can complicate debugging.

The issue lies with the VueUse useObjectUrl composable. Due to how Vue's reactive API works, refs are unwrapped within reactive objects. This results in the cardOwner.avatar property being readonly and unable to be reassigned without reassigning the entire object:

registerDataStore.cardOwner = { ...registerDataStore.cardOwner, avatar: ... }

The misuse of useObjectUrl is causing the problem. Since the blob value does not change within the scope of the then function, making it reactive serves no benefit. The correct approach would be to replace the composable with a direct assignment:

registerDataStore.cardOwner.avatar = URL.createObjectURL(newObject)

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

AJAX - Triggering a function to navigate to a different webpage

Here are two JavaScript functions I have been working with: The first function, View, allows me to navigate to a specified URL path and display the content on my view page. function View(URLPath) { $.ajax({ url: URLPath, type: "get", ...

Updating the Navbar in React Routing does not happen automatically, but it does refresh when the page is refreshed

Currently, I am using vanilla React with React-Router for my project. One of the issues I am facing is detecting whether a user on the page has a token, indicating their logged-in status. While setting up routes for Login/Register/Logout functionalities, ...

What is the best way to swap out the css selector using jQuery?

Looking to switch the height attribute to min-height This is how I usually update the value, but uncertain about changing the attribute $('.mySelector').css('height','650px') <div class="mySelector" style="cursor: -moz-g ...

What is the best way to verify if all the elements in an array of strings are present within the array?

I am working with two arrays of strings, array1 and array2, that I need to compare. I want to find a way to check if all elements from array2 are present in the elements of array1, and then create a new array based on this comparison. I believe using filte ...

What causes the object to be updated with new values when I update reference variables in JavaScript?

Imagine having an object named x with values x = { a: 'abc', b: 'jkl' }, Next, I am assigning this object x to a new local variable y using var y = x. Now, when the value of var y changes as y.a = 'pqr', y.b = 'xyz&apos ...

The error message "navigation.navigate is not a function" is encountered while working with React Native

I encountered an issue with navigation in my React application. The error message "undefined is not an object (evaluating 'navigation.navigate')" appears when attempting to navigate to another screen using a button from a different route. Here i ...

Convert XML data into a structured table format

We have an XML file named "servers.xml" that needs to be parsed. This file is located on the same server where we want it to be parsed, in the same folder. <root> <list> <server> <server name="28 Disconnects La ...

What is the best way to switch between three different menus and ensure that the last selected menu remains open when navigating to a new page

My knowledge of javascript, jquery, and php is pretty much non-existent, unfortunately. I only have a grasp on html and css at the moment. However, I will be starting school to learn these languages in the fall! The issue I am currently facing is creating ...

Discovering the method to retrieve the value of an array in JavaScript

Currently, I am developing an Android application in Titanium Studio with a Rest API (Apigee Baas) as the backend. The data stored in the Apigee Baas includes fields such as name, address, and attendance records. When retrieving the response from the Rest ...

Ways to tally elements that have been dynamically loaded onto the webpage through AJAX requests

My page has a dynamic region that is continuously updated using jQuery's .ajax and .load methods. In order to submit the form, there need to be at least 3 of these 'regions', so I have to keep track of the number of DIVs with a specific cla ...

Modifying the property value in a child component using the parent component in VueJS

Hey there, I'm facing an issue where I need to update a prop in my child component when the parent component changes the value. However, I'm attempting to do this in a unique way and running into a problem where the child prop is not being update ...

What is the best way to upload images and generate permanent links for them using Express.js?

After successfully implementing the upload-part feature to my website, I encountered an issue. When I try to upload a file that is not a picture, the website displays an empty bracket []. My intention is to reject any non-picture uploads altogether. I am ...

Can .map() be utilized to transform an array of objects into a single object?

Presented here is a subset of data extracted from a larger dataset in a project: const data = [ { scenario: '328', buckets: 2 }, { scenario: '746', buckets: 1 }, { scenario: '465&apos ...

Using jQuery to loop through a collection

I have a page that displays a list of posts. When a user clicks on the show comments button for a particular post, the comments associated with that post become visible. This functionality is achieved by using this and then searching based on the click loc ...

The module cannot be required as a function for calculating the area of a square

None of the functions I created seem to be working properly. Take a look at this example function: function calculateArea(side) { var area = side * side; return area; } When I attempt to use the module using require, like so: var formulas = require( ...

Cover a cube with a material using Three.js

After following the tutorial on creating a Cube with a texture, I encountered an issue where the texture repeated on every face of the cube. I am seeking a solution to use a single texture that 'wraps' around the cube. Is there a way to achieve t ...

Tips for creating a dynamic curved SVG path

I'm looking to draw a black border only along the inside of this SVG. I've tried adding stroke and setting the stroke-width, but that applies the border to the entire SVG. Is there a way to limit the stroke to a certain point within the SVG? d ...

Ways to Deduct 10 Days from a Date using JavaScript

After receiving some helpful suggestions, I managed to solve my date issue by using moment.js. Here is the snippet of code where I implemented moment.js: var preorderdate = moment(date).subtract('days',10).format('MMMM D, YYYY'); var r ...

PHP and jQuery AJAX for Showing and Concealing Data

I am working with a products table that includes the following columns: id (integer) name (varchar, max length 100) My goal is to dynamically display content based on the option selected. For example: <select class="form-control" name="name" require ...

Manage and modify Firestore data and Firebase storage in an asynchronous manner

I've implemented a crud system for data storage that includes images. This system consists of fields for name, type, qty, description, and url. The url field is used to store the URL of an image that has previously been uploaded. Below is the code fo ...