Skip to content

Add reference (docs) on how to delete a resource child object attribute on update #20

Open
@fabiozaffani

Description

@fabiozaffani

This is due to this specific part of the code inside the update() method:

itemRef.once('value', dataSnapshot => {
  try {
    let item = dataSnapshot.val() || {};
    let fields, removed, i;
    if (resourceConfig.relations) {
      fields = resourceConfig.relationFields;
      removed = [];
      for (i = 0; i < fields.length; i++) {
        removed.push(attrs[fields[i]]);
        delete attrs[fields[i]];
      }
    }
    DSUtils.deepMixIn(item, attrs);
    if (resourceConfig.relations) {
      fields = resourceConfig.relationFields;
      for (i = 0; i < fields.length; i++) {
        let toAddBack = removed.shift();
        if (toAddBack) {
          attrs[fields[i]] = toAddBack;
        }
      }
    }
    itemRef.set(item, err => {
      if (err) {
        reject(err);
      } else {
        resolve(item);
      }
    });
  } catch (err) {
    reject(err);
}

The DSUtils.deepMixIn will try to deep merge the updated object with the existing one, so in order to be able to remove a list of objects inside a attribute child we have to set it's value to NULL, deleting it or setting it to an empty object will just cause the deepMixIn to override the updated value with the data already existing in the database.

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions