What would cause an object property to be designated as read-only in my JavaScript (Vue) code?

In my Vue 2 application, there is a state variable named boxes which stores an array of box objects. A computed property called nearest_box_linked_boxes extracts a subset of these boxes.

There is a method that goes through the boxes obtained from nearest_box_linked_boxes and updates a property in each element:

for(let i=0;i<this.nearest_box_linked_boxes.length;i++)
{
  let box = this.nearest_box_linked_boxes[i];
  box.object_class = this.$store.state.selected_object_class;
  box.patch();
}

However, this method is throwing an error:

vue.esm.js:648 [Vue warn]: Error in v-on handler: "TypeError: Cannot assign to read only property 'object_class' of object '#<Box>'"

I did not explicitly set any of the box objects or their properties as read-only. While I understand that I cannot directly write to nearest_box_linked_boxes since it's a calculated property, I believed it should be possible to modify the properties of each element within the array.

Could this issue be related to Vue and how it handles calculated properties, or is there something else at play?

Answer №1

It is advisable to view computed properties as if they were "read-only", with the only exception being computed setters.

Although it is technically feasible to alter an object returned by a computed property, it is typically not recommended. The object will be replaced upon any change in dependency, resulting in the loss of your modifications.

Answer №2

It appears that the error was due to my own oversight. While Nikola's suggestion may be beneficial, I realized that I had unknowingly used Object.freeze in my code at one point and simply overlooked it. This issue had nothing to do with Vue or any other enigmatic factors.

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

Finding an element and applying a CSS class to it using Selenium in JavaScript

Is there a way to add a class to a specific element? I attempted to use the classList.add method in JavaScript, but it didn't work ...

Using JavaScript's regular expressions to identify a code block that commences with a specified pattern

Currently, I am working on a JavaScript script and I am in need of a Regex pattern to quickly match "JSDocs". The specific pattern that I am trying to match looks like this: # this is block1 /// text /// text /// text /// text # this is block2 /// text // ...

Issues encountered during the installation of Electron JS

Having trouble installing electronjs in Node.js 18 LTS and NPM 10? Getting an error message like this? PS C:\Users\Administrator.GWNR71517\Desktop\electron> npm install electron --save-dev npm ERR! code 1 npm ERR! path C:\Users& ...

Executing the default function for a successful response when using Jquery's $.post

Is there a way to automatically execute specific functions after every successful completion of a $.post request? For example, currently I have the following code: $.post(ROOT + 'Ajax', { call: 'Enquiry->setEnquiryUrgency', ...

How can you pause a YouTube video using jQuery?

My custom jQuery slider consists of three panels that slide smoothly by adjusting their left CSS values. Everything works perfectly, except for one issue - I have a YouTube video embedded in one of the slides, and it continues playing even when the slide i ...

Is there a way to automatically update the URL to include $_GET['data'] (media.php?data=123) when a selection is made from a dropdown list?

I'm currently working on a project that involves a website in PHP and a MySQL database. I have successfully linked the two together, and now I have a combobox filled with data from the database. Below is my script for handling the selection: <scr ...

I would like to create a fixed-position menu similar to Slashdot's comment filtration menu. How can I achieve this?

Have you ever noticed that Slashdot has a cool little feature that lets you adjust your comment threshold to filter out down-modded comments? When you scroll to the top of the page, it stays in one place, but as you continue scrolling down, it eventually s ...

Simple steps to change JSON Object to JavaScript array

Referring to this question and this other one, I am seeking advice on how to handle a JSON object structure different from the examples provided: This is my JSON Object: var myData = { "04c85ccab52880": { "name": "name1", "firstname": ...

Resetting a JQuery button to its initial state

I have a button that, when clicked, rotates to 25deg thanks to Jquery. Now, I want it so that if I click the button again, it returns to its original position. Here is what I have tried: $(document).ready(function() { $(function() { ...

Creating a 3D object using three.js framework

When playing video games, I often notice a common pattern where hovering the mouse over an object triggers a gradient highlight around its 2D representation. To recreate this effect in my Three.js scene, I started by setting up the necessary raycaster vari ...

Why do I keep receiving the error message "Cannot set property of undefined"?

In the midst of a small project that involves utilizing nuxt, js, and axios, I encountered an issue when attempting to assign the response data to my formFields object. Despite having declared formFields within the data section, I kept receiving an error m ...

Encountered a React 16 error: Unexpected TypeError stating that this.state.userInput.map is not a valid

I am working on a simple React app where a user can input text and I want to display each character as a list. Here is the progress I have made so far: App Components import React, { Component } from 'react'; import './App.css ...

My Angular 6 app kept encountering this error repeatedly, eventually causing the browser to freeze

[Violation] A non-passive event listener was added to a scroll-blocking 'touchstart' event. Consider marking the event handler as 'passive' to improve page responsiveness I have been successfully using Tabulator in Angular, but after s ...

What does it mean in Javascript when b1 is undefined while o1 has a value and is equal to b1?

Having some issues getting variables to work with drop down options on a page. At first, I couldn't even extract a value from the function but managed to do so by removing "var" from o1. Strange thing is, when I type o1 into the js console on chrome i ...

Using Vue.js, what is the best way to modify the class of a parent element based on changes made to a child

I'm utilizing Vue version 2.6.11 and I am looking to dynamically add or remove a class on the ancestor (grandparent) element of an input (checkbox) whenever its value changes. Since there is an unknown number of checkboxes, they are generated using th ...

Can you configure the redux store once and share it across different pages?

I have a redux store where the state is in the form of {names:[], address:[]}. Across multiple pages, both names and address are used as properties. However, I find myself making API calls on each page to fetch these values which is causing unnecessary n ...

Tips for customizing the appearance of the active item in the navigation bar

Currently, I am developing a project with React.js using HTML, CSS, and JavaScript. My goal is to enhance the visual appeal of the active navigation item in the navbar by adding a white border around it. This way, the border will remain visible while the c ...

What is the best way to use form input to filter an Observable?

Within my component, I have declared the variable "countries$": countries$!: Observable<Country[]>; To populate this variable with data from this API, I use the following code in the "ngOnInit" lifecycle hook: ngOnInit(){ this.countries$ ...

AngularJS Partial Views: Enhancing Your Website's User Experience

As a newcomer to the world of Angular, I am seeking guidance on a specific issue. I have encountered a one-to-many relationship scenario where one Category can have multiple Product items. The challenge lies in my layout page setup where I display various ...

What could be preventing this bootstrap carousel slider from transitioning smoothly?

I've created a CodePen with what I thought was a functional slider, but for some reason, the JavaScript isn't working. The setup includes bootstrap.css, jquery.js, and bootstrap.js. I'm having trouble pinpointing what's missing that&apo ...