Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inserting encapsuled object in firestore and goods practices

Tags:

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

like image 642
Diego Venâncio Avatar asked Oct 21 '17 23:10

Diego Venâncio


People also ask

Can you store objects in firestore?

Cloud Firestore is optimized for storing large collections of small documents. All documents must be stored in collections. Documents can contain subcollections and nested objects, both of which can include primitive fields like strings or complex objects like lists.

How do I speed up firestore queries?

One simple option is to add limits to your queries. If you suspect that your user only needs the first handful of results from your employee list, add a limit(25) to the end of your query to download just the first batch of data, and then only download further records if your user requests them.


1 Answers

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)); 
like image 161
Mateus Forgiarini da Silva Avatar answered Sep 23 '22 20:09

Mateus Forgiarini da Silva