5

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.person
//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

Diego Venâncio
  • 5,698
  • 2
  • 49
  • 68
  • 1
    Did you manage to solve this? I have the same problem when saving a custom object, even though Google says it should be possible - https://firebase.google.com/docs/firestore/manage-data/add-data – Paul Strupeikis Nov 01 '17 at 23:33
  • I continue inserting with {key: values} way: onsubmit() { this.db.collection("user").add({ name: this.form.get('name').value, email: this.form.getemail: ... – Diego Venâncio Nov 01 '17 at 23:57

1 Answers1

4

Firestore only accepts a JavaScript object embedded within a document if it is a “pure” object, this means you can't use custom objects while coding with TypeScript.

Change your code to:

this.db.collection("person").add(Object.assign({}, this.person));