DEV Community ๐Ÿ‘ฉโ€๐Ÿ’ป๐Ÿ‘จโ€๐Ÿ’ป

T-Kisaku
T-Kisaku

Posted on

Firestore error: Error: Value for argument "data" is not a valid Firestore document. Input is not a plain JavaScript object.

Hello!!
I'm trying to use firestore as database of my service api. But it was occured error like below.

Error: Value for argument "data" is not a valid Firestore document. Input is not a plain JavaScript object.
Enter fullscreen mode Exit fullscreen mode

Approach


import { DocumentData, FirestoreDataConverter, QueryDocumentSnapshot } from '@google-cloud/firestore'

export const converter = <
    AppValue extends object
>(validator?: (value: AppValue) => AppValue): FirestoreDataConverter<AppValue> => {
    return {
        fromFirestore: (snapshot: QueryDocumentSnapshot<AppValue>) => {
            let data = snapshot.data()
            Object.keys(data).forEach((key) => {
                // @ts-ignore
                if (typeof data[key].toDate == "function" && typeof data[key].seconds == "number") {
                    // Timestampๅž‹ใฏๆ‰ฑใ„ใซใใ„ใฎใงDateๅž‹ใซๅผทๅˆถๅค‰ๆ›ด
                    // @ts-ignore
                    data[key] = data[key].toDate()
                }
            })
            return data
        },
        /*
        * Async Await syntax don't support!!
        *
        * Fix like below
        * toFirestore: (value: AppValue): DocumentData => {
        **/
        async toFirestore: (value: AppValue): DocumentData => {
            if (validator) {
                return validator(value)
            }
            return value
        }
    }
}

export default converter

Enter fullscreen mode Exit fullscreen mode

Oldest comments (0)

We are hiring! Do you want to be our Senior Platform Engineer? We're hiring for a Senior Platform Engineer and would love for you to apply.

Head here to learn more about who we're looking for.