Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript object retaining "old" properties, can't override?

I have the following code:

    const readDataFromSql = () => {
        // going to have to iterate through all known activities + load them here
        let sql = "[...]"
        return new Promise((resolve, reject) => {
            executeSqlQuery(sql).then((dict) => {
                let loadedData = [];
                for (let key in dict) {
                    let newItemVal = new ItemVal("reading hw", 7121, progress.DONE);
                    loadedData.push(newItemVal);
                }
                resolve(loadedData);
            });
        });
    }

ItemVal implementation:

class ItemVal {
   constructor(name, time, type) {
      this.name = name
      this.time = time
      this.type = type 
   }
}

Let's assume that newItemVal = "reading hwj", 5081, progress.PAUSED when readDataFromSql() first runs.

readDataFromSql() is then again called after some state changes -- where it repulls some information from a database and generates new values. What is perplexing, however, is that when it is called the second time, newItemVal still retains its old properties (attaching screenshot below).

Am I misusing the new keyword? enter image description here

like image 437
dannybess Avatar asked Jun 02 '20 00:06

dannybess


People also ask

How can we copy object without mutating?

Similar to adding elements to arrays without mutating them, You can use the spread operator to copy the existing object into a new one, with an additional value. If a value already exists at the specified key, it will be overwritten.

How do I remove all properties of an object?

Use a for..in loop to clear an object and delete all its properties. The loop will iterate over all the enumerable properties in the object. On each iteration, use the delete operator to delete the current property. Copied!

How do you remove the property name from this object?

Remove Property from an Object The delete operator deletes both the value of the property and the property itself. After deletion, the property cannot be used before it is added back again. The delete operator is designed to be used on object properties. It has no effect on variables or functions.

How do you remove property name from object JavaScript?

The JavaScript delete operator removes a property from an object; if no more references to the same property are held, it is eventually released automatically.


Video Answer


1 Answers

From what I can see in your example code, you are not mutating existing properties but creating a new object with the ItemVal constructor function and adding them to an array, that you then return as a resolved promise. Are you sure the examples you give a correct representation of what you are actually doing

Given that, I'm not sure what could be causing the issue you are having, but I would at least recommend a different structure for your code, using a simpler function for the itemVal.

Perhaps with this setup, you might get an error returned that might help you debug your issue.

const itemVal = (name, time, type) => ({ name, time, type })

const readDataFromSql = async () => {
  try {
    const sql = "[...]"
    const dict = await executeSqlQuery(sql)

    const loadedData = dict.map((key) =>
      ItemVal("reading hw", 7121, progress.DONE)
    )

    return loadedData
 

  } catch (error) {
    return error
  }
};

If the issue is not in the function, then I would assume that the way you handle the data, returned from the readDataFromSql function, is where the issue lies. You need to then share more details about your implementation.

like image 99
Einar Ólafsson Avatar answered Oct 28 '22 07:10

Einar Ólafsson