Good Morning,
I tried adding a new created object from this class:
export class Sponsor implements ISponsor {
title: string;
description?: string;
creation: ICreation;
constructor(title: string, description: string, author: string) {
this.title = title;
this.description = description;
this.creation = new Creation(author);
}
}
and in my service, the create function looks like:
createSponsor(sponsor) {
sponsor.id = this.afs.createId();
return this.collection.doc(sponsor.id).set(sponsor);
}
When I try it this way, I get the following error:
FirebaseError: [code=invalid-argument]: Function DocumentReference.set() called with invalid data. Data must be an object, but it was: a custom Sponsor object
How can I solve this issue?
Thx to Fabian Wiles - I got it!
just save an object like this:
It's really strange behavior from firebase. And that how I fixed it - by creating new Interface and adding convertation method to my class:
Now, I can do
If you use Angular and AngularFire2, you can use AngularFirestype. This module is meant to replace AngularFirestore and allow to get and set data to Firestore directly with custom objects.
To do so, 3 steps are required:
1. Install angular-firestype
2. Initialize AngularFirestype module with a mapping object
3. Inject AngularFirestype service
You can basically use AngularFirestype like you use Angularfirestore.
For more details, see the homepage here: https://github.com/bricepepin/angular-firestype.
You could also serialize your object into JSON and deserialize it back into a regular JavaScript object like
works with deep nesting.
Firestore does not support that. But you can use https://github.com/typestack/class-transformer It works perfectly fine for us.
You can also use Object.assign({}, sponsor)
so in yor case it would be