fireorm icon indicating copy to clipboard operation
fireorm copied to clipboard

Complex data types couldn't serialize

Open sc-cmendoza opened this issue 3 years ago • 7 comments

I had read the documentation about complex data types, however I can't make it works.

I have this:


class GeoP{
    latitude: number;
    longitude: number;
}

@Collection()
class SomeModel{
    @Type(() => GeoP)
    geop: GeoP;
}

And then I do this:

const someGeoPInstance = new GeoP();
someGeoPInstance.latitude = 19.3753433;
someGeoPInstance.longitud = -99.0438667;

const someModelInstance = new SomeModel();
someModelInstance.geop = someGeoPInstance;

getRepository(SomeModel).create(someModelInstance);

And then I get this

Value for argument "data" is not a valid Firestore document. Couldn't serialize object of type "GeoP" (found in field "geop"). Firestore doesn't support JavaScript objects with custom prototypes (i.e. objects that were created via the "new" operator).

Is there something I'm misunderstanding?

sc-cmendoza avatar Mar 02 '21 21:03 sc-cmendoza

Hey! I think tit should work.

Can you create a minimal repo reproducing the issue so I can check it out?

wovalle avatar Mar 08 '21 17:03 wovalle

This error is thrown by firestore sdk

import * as admin from 'firebase-admin';

class Hello {
}

admin.firestore()
  .collection('foo')
  .doc('test_1')
  .set({
    prop: new Hello()
  })
  .then(console.log)
  .catch(console.error);

Output:

Error: Value for argument "data" is not a valid Firestore document. Couldn't serialize object of type "Hello" (found in field "prop"). Firestore doesn't support JavaScript objects with custom prototypes (i.e. objects that were created via the "new" operator).
    at validateUserInput ...

@wovalle , it can be fixed by serializing entity to plain object before passing to firestore SDK, e.g. JSON.parse(JSON.stringify(doc))

skalashnyk avatar Mar 11 '21 12:03 skalashnyk

This is not necessary.

Please read docs: https://fireorm.js.org/#/README?id=usage

leandrogehlen avatar Mar 11 '21 13:03 leandrogehlen

I'll take a look at your suggestion @skalashnyk

wovalle avatar Mar 12 '21 18:03 wovalle

I'll take a look at your suggestion @skalashnyk

wovalle avatar Mar 12 '21 19:03 wovalle

If I'm not mistaken, my pull request (https://github.com/wovalle/fireorm/pull/251) solves this issue.

danieleisenhardt avatar Apr 27 '21 11:04 danieleisenhardt

Does it? My initial idea with this issue was to create a generic mapping between firestore "primitives" and fields in custom models.

Can you provide an example of how would you map a native Reference or GeoPoint with the serialization included in the PR? @danieleisenhardt

wovalle avatar May 11 '21 21:05 wovalle