Skip to content

Should the $key property be deleted if it is null or undefined before sending to Firebase? #197

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

Closed
theblang opened this issue May 30, 2016 · 3 comments

Comments

@theblang
Copy link

For example, when pushing to a FIrebaseListObservable. It would be nice to model Firebase data as classes in TypeScript, but an optional $key property will be null when creating a new instance and that causes an error with Firebase.

@davideast
Copy link
Collaborator

Can you provide a sample of what you're trying to achieve so we're on the same page?

But what I think you're running into is the fact that the Realtime Database won't accept $ in keys, which is why we use $key and $value.

If you want to use a class I would create a method that drops the $key from the object and saves it to the database. Look at the example below.

http://jsbin.com/cagudocofa/edit?js,output

class Item {
  $key?: string;
  name?: string;

  /**
  * Create an Item, with no depencies on a DataSnapshot.
  */  
  constructor(key: string, data: Item) {
    this.$key = key;
    this.name = data.name;
  }

  /**
  * Create a reference for the item in the Realtime Database
  */
  ref() {
    return firebase.database().ref().child('items').child(this.$key);
  }

  /**
  * Override the toJSON method to delete the $key
  */
  toJSON() {
    const copy = Object.assign({}, this);
    delete copy.$key;
    return copy;
  }

  /**
  * Write the record in the Realtime Database, without the $key
  */
  save() {
    this.ref().set(this.toJSON());
  }
}

const item = new Item('1', { name: 'itemOne' });
item.save();

@davideast
Copy link
Collaborator

Unless there's any other items for discussion, I'm going to close this issue. Feel free to keep responding though :)

@theblang
Copy link
Author

theblang commented Jun 4, 2016

Thanks @davideast for your example! To answer your question, I was wanting to create instances of a model class using the Firebase data that was coming in, and likewise use those instances when updating with the AngularFire2 API. What I noticed is that when I made a new model instance (and the $key was null or undefined depending on how I specified the definition in the constructor), then the Firebase library itself would complain. So, my initial thought had been to delete the $key property if it was null or undefined at the AngularFire2 level to avoid having to do something like override toJSON. That change could be bad for reasons I don't realize though.

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

No branches or pull requests

2 participants