IjzerenHein / firestorter

Use Google Firestore in React with zero effort, using MobX 🤘
http://firestorter.com
MIT License
378 stars 50 forks source link

Error using a superstruct schema with a date field #50

Closed MichZipp closed 6 years ago

MichZipp commented 6 years ago

Hi,

I have the following Document Definition:

class ItemRequest extends Document {
    constructor(source, options) {
        super(source, {
            ...(options || {}),
            schema: struct({
                itemid: "string",
                startdate: "date",
                enddate: "date"
            })
        });
    }
}

If I add an Item with following structure:

var request =  {
            itemid,
            startdate: new Date(),
            enddate: new Date(),
};

I get the following error:

Error: Invalid value at "enddate" for ItemRequest with id "SCihBVAibdRAQEKDrnlW": Expected a value of type date for enddate but received {"seconds":1541717999,"nanoseconds":999000000}.

But If i check the table on firebase, I can see, that the item was added in the correct format to the database: unbenannt

Anyone an idea, what could couse this error?

Greeting, Michael

IjzerenHein commented 6 years ago

Hi Michael. This is indeed a limitation/bug at the moment. Firestore switched their date data-type to timestamp a while back. While writing to Firestore using a Date still works, when reading data, the new timestamp is returned. This probably happens when you set the timestampsInSnapshots setting during initialisation. Of course, since the timestamp data-type is considered to be the new high fidelity date-time type, this should obviously be supported by Firestorter. I will try to add a solution for this in the next couple of weeks. Cheers, Hein

IjzerenHein commented 6 years ago

Hi Michael. I just released a new version of Firestorter which resolves this issue: https://github.com/IjzerenHein/firestorter/releases/tag/v1.1.0

Please view these docs on how to use timestamps in schemas: https://github.com/IjzerenHein/firestorter/blob/master/docs/SchemasAndCustomDocuments.md#timestamps