Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
fabiozaffani opened this issue Nov 22, 2015 · 3 comments
Assignees
Labels

Comments

@fabiozaffani
Copy link

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.

@fabiozaffani
Copy link
Author

Related to #22

@jmdobry
Copy link
Member

jmdobry commented Sep 20, 2016

This would be solved by fixing #22 right?

@fabiozaffani
Copy link
Author

@jmdobry I think it wouldn't, the logic being applied here is to fetch the current data from firebase and merge with the new one being passed before setting it back to firebase. However, if the data was directly set in the firebase database with, for example, an attribute/property set with an empty object {}, it would delete the field at firebase, while doing the same thing here it would not work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants