Inserting encapsuled object in firestore and goods practices

I have a question about to insert object in firestore in angularfire:

My object Person.ts

name: String
age: Number
//--constructor--
//--getters and setters--

if I do this, insert ok: (BUT is this good practice?)

[person.component.ts]
      this.db.collection("person").add({
              name: this.person.$nome,
              age: this.person.$email
          })
    ...

but if I try:

    [person.component.ts]
         this.db.collection("person").add({
                     Person: this.entrevistador
//or this this.person
                  })

I get this error in browser console:

Function DocumentReference.set() called with invalid data. Unsupported field value: a custom Person object (found in field Person)
at new FirestoreError (error.js:149)
at

Source: AngularJS

Comment (2)

  • Johnny Blackboard| April 7, 2018

    You can serialize and deserialize your class to return an anonymous object with JSON.stringify and JSON.parse

    It will work – but it’s a bit hacky.

    this.db.collection(“person”).add({
    Person: JSON.parse(JSON.stringify(this.entrevistador))
    });

  • Felicia Dolittle| April 7, 2018

    Your first attempt is probably the right way to go. Don’t use Johnny’s way, bad practice

  • Leave a Reply

    Your email address will not be published. Required fields are marked *

    This site uses Akismet to reduce spam. Learn how your comment data is processed.