Implementing new updates to an object without affecting existing values in Vue.js 2

I am currently facing an issue with updating values for an object received in a payload before saving it to the database. Despite making changes, the new values are not being persisted correctly. I am utilizing Vue.js 2 for this task. How can I effectively update the incoming object and store those updated values in an existing object?

Additional details: The data we receive from an API may contain existing key-value pairs or none at all depending on certain criteria such as username, birthday, or phone number. The goal is to update the personal information fields with any new values. However, instead of updating the new values, the data retains old changes without incorporating the updates. In this case, userPersonalInfo is not being updated.

ModalVerification.vue

onVerifySuccess(existingData) {
 // if no object exists, complete new form 
 if(!Object.keys(existingData).length) {
   this.completeFormModal();
  } else {
   this.meetingDetails.is_authenticated_user = true;
   this.updateMeetPlanInformation(this.getMeetingPlanFields(existingData));

   // only return existing data if object is not null; otherwise, update the existing data with new key/value pairs. This approach seems incorrect as it does not verify if any values have been updated before passing them.
   const userPersonalInfo = (existingData === null) ? this.getUserPersonalInfo(existingData) : existingData;

vueAssign(this.meetingDetails, userPersonalInfo);
this.completeFormModal();
}

export function vueAssign(objVal, srcVal) {
 Object.keys(srcVal).forEach((key) => {
   Vue.set(objVal, key, srcVal[key]);
 });
}

Answer №1

The issue may lie within the vueAssign function, but without seeing that method I can still provide some potential solutions:

Object.assign

You can utilize the Object.assign method to transfer properties from meetingDetails to userPersonalInfo, replacing any matching properties:

Object.assign(userPersonalInfo, this.meetingDetails)

const userPersonalInfo = {
  a: 1,
  b: 2,
}

const meetingDetails = {
  a: 999,
  c: 'hello',
}

Object.assign(userPersonalInfo, meetingDetails)

console.log({ userPersonalInfo })

Spread operator

Another option is using the spread operator, which achieves the same result:

let userPersonalInfo = /*...*/

userPersonalInfo = {
  ...userPersonalInfo,
  ...this.meetingDetails,
}

let userPersonalInfo = {
  a: 1,
  b: 2,
}

const meetingDetails = {
  a: 999,
  c: 'hello',
}

userPersonalInfo = {
  ...userPersonalInfo,
  ...meetingDetails,
}

console.log({ userPersonalInfo })

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

Peer dependency conflict encountered during installation of Bootstrap and Bootstrap-Vue

Encountering issues while trying to start a new Vue Bootstrap project. Here is my process and the errors I am facing, any suggestions? Unfortunately, Chat-GPT was not helpful, so now I'm turning to you for assistance :-) vue create my-project select ...

What is the method for obtaining the object of the chosen Vuetify tab instead of its index value?

By default, the event triggered by changing the v-tabs provides the index of the new tab. However, I am encountering issues with obtaining the correct index when the tab items are dynamic. <v-tabs :slider-size=4 v-model="tabs" @change="tabChangeHandler ...

Error: Discord.js was unable to access the 'id' property because it was undefined

I need help with my ticket system project where I am trying to create a channel and send an embed in that channel. However, when I run the code, I encounter the error message TypeError Cannot read property 'id' of undefined. Here is the snippet ...

What causes the toggle effect in my jQuery onclick function to alternate between on and off when the initialization is repeated multiple times?

I am facing an issue with my website where icons/buttons trigger a menu when clicked. I need to load more data by adding more buttons, so I tried re-initializing the existing buttons using a jQuery onclick function whenever the number of buttons changes. ...

parsing a TypeScript query

Is there a simpler way to convert a query string into an object while preserving the respective data types? Let me provide some context: I utilize a table from an external service that generates filters as I add them. The challenge arises when I need to en ...

Deploying an Angular application created using Yeoman to Heroku

I have been following some instructions on how to deploy my project, such as this guide or this tutorial. However, I am unable to get it to work properly. This is the code in my server.js file: var app, express, gzippo, morgan; gzippo = require('gz ...

Is it possible to globally delay the execution of WebElement.sendKeys() in Protractor's onPrepare function?

While running protractor on a sluggish machine, I am in need of slowing down each key press and action performed. The action part has been successfully implemented, but how can I achieve the same for key presses? I have come up with a local solution which ...

Leverage Arrays with Bootstrap SelectPicker in Javascript

I am looking to populate a Bootstrap SelectPicker with values from an array. I am unsure of how to loop through the array to extract the values. Currently, I have manually added values to the SelectPicker like this. <!DOCTYPE html> <html> < ...

Does Vuex dispatch from within a component include a particular type of behavior known as a "promise"?

Currently, I am diving into vuex and facing an issue. During the created() lifecycle hook, my goal is to fetch data from an API. Once this action is complete, I need to call a getter from the component and assign the retrieved cards to the component's ...

How can I retrieve the /api/auth/me resource serverside using the NextJS AppRouter?

I am looking to implement app router in my Next.js project and have encountered an issue. In order for my app to function properly, I need to make a call to /api/auth/me which will return either a user object or null if the user is not logged in. To achiev ...

Can someone help me troubleshoot the issue with my submit button's onclick function?

I am currently working on a project where I have a content box enclosed in a div tag. Within this content box, there are paragraphs with unique IDs for individual styling rules. I have set margins, padding, and other styles accordingly. At the bottom of th ...

Encountering SCRIPT errors when utilizing Babel and Vue.js on IE 11

We're encountering an issue with our Vue.js application specifically on Windows 10 using IE 11. Initially, we were facing a SCRIPT1003: Expected ':' error until we made adjustments to our babel.config as shown below: module.exports = { p ...

A compilation of category listings derived from two arrays of objects that share a common parent ID

I have a challenge with two arrays of objects connected by a parent ID. My goal is to create a categorized list where each category contains the corresponding data set. The structure should consist of a header (category) followed by buttons (data) related ...

employ varying XSL stylesheets to display an XML document in a separate window

My issue involves an XML file being transformed by a specific XSL file (XSLT 1.0), which in turn includes multiple other XSL files with various templates. The challenge is to add a button to the rendered XML that, when clicked, opens the same XML in a ...

Establish individual states for every dynamically created component using the same handler function

I have a component in React built with Material UI, where the child component (Paper) is dynamically generated depending on the number of items in an array. The challenge I'm facing is changing the elevation property of the Paper component when it&ap ...

Converting JSON-style data into a string with the power of node mysql

Just a quick note - I'm a total newbie in the world of web development, so I might be missing an obvious solution here. My challenge is to insert a dataset into a MySQL database using nodejs/expressjs/mysql. I've managed to establish a connecti ...

Conditional rendering of a component in ReactJS while maintaining the "empty" space

Is there a way to conditionally render a component without affecting the layout of other components? I'm trying to avoid unwanted shifts caused by this div Do you have any suggestions? Here is a snippet of my code: const [displayFeedbackMessage, s ...

The random quote generator or current tweet quote feature is malfunctioning

I am having trouble with a jQuery on click function that is not working to tweet the current quote. I am working on building a random quote machine and tweeting the current quote is essential. Despite successfully implementing the JSON API, I cannot seem t ...

What's causing my variables to be returned as null in the alerts?

Why am I receiving null values when alerting my variables? I'm attempting to pass a string stored in a variable from an external JavaScript file back to the main page using alerts. Initially, I suspected that the JavaScript was not fetching data cor ...

Storing information in the DOM: Choosing between Element value and data attribute

When it comes to storing values in a DOM element, we have options like using the data attribute. For example, $("#abc").data("item", 1) can be used to store values and then retrieve them with $("#abc").data("item"). However, I recently discovered that th ...