Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

toFirestore inside globalFirestoreOptions.converter is never called #1497

Closed
mrleblanc101 opened this issue Feb 25, 2024 · 1 comment
Closed

Comments

@mrleblanc101
Copy link

mrleblanc101 commented Feb 25, 2024

Reproduction

.

Steps to reproduce the bug

I created this global converter, the fromFirestore gets called, but not the toFirestore.
I see test 2 in the console, but never test 1, and the data in my firestore does not have the field created_at.

globalFirestoreOptions.converter = {
    toFirestore(docData: any) {
        console.log('test 1');
        return {
            created_at: serverTimestamp(),
            ...docData,
        }
    },
    fromFirestore(snapshot: QueryDocumentSnapshot, options: SnapshotOptions): DocumentData {
        console.log('test 2');
        const data = firestoreDefaultConverter.fromFirestore(snapshot)!;
        return data;
    },
};

Expected behavior

Both toFirestore and fromFirestore should be called.

Actual behavior

Only the fromFirestore is called.

Additional information

I want to automatically add a created_at field so that I can sort by date when fetching.

Important: Unlike "push IDs" in the Firebase Realtime Database, Cloud Firestore auto-generated IDs do not provide any automatic ordering. If you want to be able to order your documents by creation date, you should store a timestamp as a field in the documents.
Ref: https://firebase.google.com/docs/firestore/manage-data/add-data?hl=fr

@mrleblanc101
Copy link
Author

It works if I use .withConverter instead.
Exemple:

const addDefaultFields = {
    toFirestore(docData: DocumentData): DocumentData {
        console.log('test 1');
        return {
            created_at: serverTimestamp(),
            ...docData,
        }
    },
    fromFirestore(snapshot: QueryDocumentSnapshot, options: SnapshotOptions): DocumentData {
        console.log('test 2');
        const data = firestoreDefaultConverter.fromFirestore(snapshot)!;
        return data;
    },
};

...

async function addProject(option: Project) {
    const project = await addDoc(collection(db, 'projects').withConverter(addDefaultFields), {
        name: option.name,
        user: user.value!.uid,
    })
    return { id: project.id, name: option.name };
}

@mrleblanc101 mrleblanc101 changed the title Global toFirestore never gets called toFirestore inside globalFirestoreOptions.converter is never called Feb 25, 2024
@vuejs vuejs locked and limited conversation to collaborators Feb 25, 2024
@posva posva converted this issue into discussion #1499 Feb 25, 2024

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant