I want to define a changeable variable and merge table.changeSet into table.data. The main goal is: change only changed JSON fields and then save to database.
I have categoriesTable that contains two fields: name and weight. Then I save these data in JSONB format in my PostgreSQL database.
For example, the categoriesTable.data is [{"name": "nature", "weight": 0.8}, {"name": "climbing", "weight": 0.8}]. Then I changed the second record weight with name climbing from 0.8 to 0.82 in the categoriesTable and in result got categoriesTable.changeSet
. But when I'm trying to execute the next query:
// Reference external variables with curly brackets or using JS variables
var data = categoriesTable.data;
const changeset = categoriesTable.changeSet;
changeset.forEach(function(change) {
for (var key in change) {
var index = key.toString();
if (data[index]) {
Object.assign(data[index][key], change[key]);
} else {
data[index] = change[key];
}
}
});
return data;
I got `message:"changeset.forEach is not a function"`