VueJS throws an error indicating that the object cannot be extended

I have encountered an issue while trying to update the promo object by adding a new field called colspan. Unfortunately, I am getting this error:

uncaught (in promise) TypeError: Cannot add property colspan, object is not extensible at eval (eval at (http://localhost:8080/app.js:3160:1), :71:32) at Array.forEach (native) at eval (eval at (http://localhost:8080/app.js:3160:1), :66:35) at Array.forEach (native) at eval (eval at (http://localhost:8080/app.js:3160:1), :64:47) at Array.forEach (native) at eval (eval at (http://localhost:8080/app.js:3160:1), :62:23) at Promise () at F (eval at (http://localhost:8080/app.js:865:1), :35:28) at Object.modifyData (eval at (http://localhost:8080/app.js:3160:1), :48:12)

departmentIds.forEach(departmentId => {
          results[departmentId] = []
          departmentWiseResults[departmentId].forEach((promo, index) => {
            let tmpPromo = promo
            dateRanges.dateRanges.forEach(range => {
              let startedDateWeek = moment(promo.startDate).week()
              let endDateWeek = moment(promo.endDate).week()
              let startedYear = moment(promo.startDate).year()
              let endedYear = moment(promo.endDate).year()
              tmpPromo.colspan = 0
              if (range.startYear === startedYear && range.endYear === endedYear && range.weekNumber <= endDateWeek && range.weekNumber >= startedDateWeek) {
                tmpPromo.colspan++
              }
              departmentWiseResults[departmentId].splice(index, 1, tmpPromo)
              console.log('stareted:', startedDateWeek, endDateWeek, startedYear, endedYear, promo, tmpPromo, departmentWiseResults[departmentId])
            })
            console.log('promo after adding colspna:', promo)
            // if (isInRange(range, promo)) {
            //   console.log('for range', range.startDate, range.endDate)
            //   console.log('for promo', promo.id)
            //   console.log(
            //     'diff is',
            //     findWeekspan(range, dateRanges.dateRanges[dateRanges.dateRanges.length - 1], promo)
            //   )
            // // if (!promo.used) {
            // //   promo.used = true
            // //   results[departmentId]
            // // }
            // }
          })
        })

Please lend me your expertise in resolving this issue.

Answer №1

The variable promo has been made non-extensible using Object.preventExtensions (or seal or freeze). This means that new properties cannot be added to it. Instead, a new copy of it with additional properties can be created. The easiest way to achieve this is by using Object.assign.

let tmpPromo = Object.assign({colspan: 0}, promo);

This code snippet creates an anonymous object with the property colspan, copies properties from promo into it, and then returns the resulting object as tmpPromo.

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

using document references in puppeteer's evaluate/waitFor function

I'm feeling a bit lost when it comes to understanding the document reference in puppeteer's evaluate method. The official documentation shows some code that includes this reference within a waitFor function in a node script. I get that these line ...

Error in Angular 4: Undefined property 'replace' causing trouble

I've been trying to use the .replace() JavaScript function in Angular 4 to remove certain characters from a string. Here is the code snippet from my component: @Component({...}) export class SomeComponent implements OnInit { routerUrl: string = &apo ...

Could someone kindly clarify the workings of this jQuery script for me?

I found this code online, but I'm struggling to comprehend its functionality. In order to make any edits for my needs, I need to understand how it operates. The purpose of this script is to close a panel with an upward slide animation when the "x" but ...

Utilizing Bootstrap Vue to retrieve a specific object from an array of objects using the b-form-select component

While I was able to find a solution to my issue, I can't help but feel slightly unsatisfied with the complexity of the current method. Is there a simpler way to achieve this? Ideally, I would like to have similar HTML code where all I need to do is ma ...

A guide to implementing div wrapping in HTML

I'm in the process of using flexbox on my website. I have 10 div elements that I want to wrap around each other closely without any gaps between them. My goal is to have "4" positioned to the right of "1", instead of creating a new row and moving dow ...

Ensure to trigger the Ajax function prior to the tab change event in a Vue form wizard

I'm currently utilizing the Vue wizard from https://www.npmjs.com/package/vue-form-wizard and I need to trigger an AJAX function before the tab change event like so: beforeTabSwitch: function(tab){ if(tab===2) { this.formData .post(APP_URL ...

Increase the jQuery Array

After successfully initializing an Array, how can I add items to it? Is it through the push() method that I've heard about? I'm having trouble finding it... ...

The value of the cookie is not set (version 2.0.6 of react-cookie)

I am encountering an issue with implementing react cookies version 2. I am using webpack-dev-server for testing. Below is the console log: Warning: Failed context type: The context cookies is marked as required in withCookies(App), but its value is unde ...

Close to completing the AngularJS filter using an array of strings

I'm currently working on developing a customized angular filter that will be based on an array of strings. For instance: $scope.idArray = ['1195986','1195987','1195988'] The data that I aim to filter is structured as fo ...

Display Nvd3 Pie Chart percentages in decimal format

I have integrated Nvd3 into my Angular project to create various types of charts. Utilizing the angular directive from Krispo's website, I am currently working on a pie chart that displays values in percentages. However, the displayed values are round ...

What is the recommended lifecycle hook in Vue.js2 to execute a function when the page is loaded?

I have a dynamic table that can be filled with various numbers of rows, and I want to add an overlay before the data is loaded using my applyOverlay() function. Below is the structure of my HTML: <table id="table" class="datatable" s ...

Are you integrating xdebug with a single-page application built on front-end JavaScript?

My xdebug works perfectly when I access my server directly through a vhost entry to the IP configured by homestead/vagrant. However, I am currently using a decoupled frontend on localhost:8080 that communicates with the Laravel backend server, but unfortun ...

Unable to trap error using try-catch block within an asynchronous function

I'm attempting to integrate a try-catch block into an async function, but I am having trouble catching errors with status code 400 using the code below. const run = async () => { const response = await client.lists.addListMember(listId, { ema ...

The error message "Evaluating this.props.navigation: undefined is not an object" indicates that there

In the navigation bar, I am trying to keep a touchable opacity at the top right. When this touchable opacity is pressed, I want to redirect the user to the home page. constructor(props) { super(props); this.state = { stAccntList: [], ...

Output JSON data to an HTML5 table

Here is a code snippet to retrieve data: function fetchInformation(){ $.get('http://mywebsite.net/getFile.php', function(data) { var result = JSON.parse(data); } The returned JSON data is as follows: Object {0: "11", 1: ...

Leveraging the import statement within lib.d.ts to enhance Intellisense functionality in Visual Studio Code

Looking to streamline my JavaScript project by utilizing custom global variables and harnessing the power of VSCode intellisense for auto completion. Here's what I'm aiming for: See example of auto completion for 'lol' After some sear ...

Fetching an item from Local Storage in Angular 8

In my local storage, I have some data stored in a unique format. When I try to retrieve the data using localStorage.getItem('id');, it returns undefined. The issue lies in the way the data is stored within localStorage - it's structured as a ...

Finding an element that lacks both a class and an id, and is not consistently present - what's the trick?

Currently, I am faced with a predicament in my code where a <li> element only appears under specific conditions, making it difficult to apply positioning. This element lacks an id and class attribute, which prevents me from targeting it accurately us ...

Extracting a particular element from a sophisticated auto-complete DOM structure by drilling down into the $scope

After spending a significant amount of time trying to solve this problem, I find myself at a dead end. The simplicity of using jQuery makes me reconsider Angular, but perhaps my approach is flawed. In this scenario, the DOM structure looks like this: < ...

What is the most effective way to reduce the number of http requests caused by onChange events in Material UI Slider?

Is there a way to optimize my request handling? In my React project, I am currently utilizing Material UI's slider. With the onChange property, I am making HTTP POST requests whenever the slider is moved. Here is a snippet of the code: <Slider ...