Skip to content

FirestoreDataConverter example doesn't work. #7671

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

Closed
marcusx2 opened this issue Oct 2, 2023 · 3 comments · Fixed by #7673
Closed

FirestoreDataConverter example doesn't work. #7671

marcusx2 opened this issue Oct 2, 2023 · 3 comments · Fixed by #7673

Comments

@marcusx2
Copy link

marcusx2 commented Oct 2, 2023

Operating System

macOS

Browser Version

Safari 16.5.2

Firebase SDK Version

10.4.0

Firebase SDK Product:

Firestore

Describe your project's tooling

index.html with source tag. Just javascript.

Describe the problem

This example does not work at all. The thrown error is TypeError: collection(db,'posts').withConverter(postConverter).doc is not a function. (In 'collection(db,'posts').withConverter(postConverter).doc()', 'collection(db,'posts').withConverter(postConverter).doc' is undefined.

The example is probably from the v8 version of the library. Can someone provide the v9 version of the example here until the docs get patched?

Steps and code to reproduce issue

class Post {
    constructor(title, author) { }

    toString() {
        return this.title + ', by ' + this.author;
    }
}

const postConverter = {
    toFirestore(post) {
        return { title: post.title, author: post.author };
    },
    fromFirestore(
        snapshot,
        options
    ) {
       const data = snapshot.data(options);
       return new Post(data.title, data.author);
    }
};

const db = getFirestore();
const postSnap = await collection(db,'posts').withConverter(postConverter).doc().get();

const post = postSnap.data();
if (post !== undefined) {
    post.title; // string
    post.toString(); // Should be defined
    post.someNonExistentProperty; // TS error
}
@marcusx2 marcusx2 added new A new issue that hasn't be categoirzed as question, bug or feature request question labels Oct 2, 2023
@MarkDuckworth MarkDuckworth self-assigned this Oct 3, 2023
@jbalidiong jbalidiong added needs-attention and removed new A new issue that hasn't be categoirzed as question, bug or feature request labels Oct 3, 2023
@marcusx2
Copy link
Author

marcusx2 commented Oct 3, 2023

I tried modifying the code with the following (everything before is the same)

const col = await collection(db, 'posts').withConverter(postConverter);
const queryDocumentSnapshot = (await getDocs(col)).docs[0];
const doc = queryDocumentSnapshot.get(new FieldPath(queryDocumentSnapshot._key.path.segments));//undefined
console.log(queryDocumentSnapshot.data());//empty post
image

but apparently it's still wrong because I can't retrieve the document. This is what I have
image

@dconeybe
Copy link
Contributor

dconeybe commented Oct 5, 2023

@marcusx2 Thank you for pointing out this obvious issue with the documentation. I have opened a PR that re-writes the sample code. If you have some time, please take a look and let me know what you think. I added a "simple" converter and a "complex" converter sample: #7673

@marcusx2
Copy link
Author

marcusx2 commented Oct 5, 2023

Looks good to me! I hope it gets merged sooner rather than later.

@firebase firebase locked and limited conversation to collaborators Jan 12, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants