-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
Comments
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 If you want to use a class I would create a method that drops the 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(); |
Unless there's any other items for discussion, I'm going to close this issue. Feel free to keep responding though :) |
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 |
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.The text was updated successfully, but these errors were encountered: