Skip to content

FirestoreDataConverter example doesn't work. #7671

@marcusx2

Description

@marcusx2

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
}

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions