[go: nahoru, domu]

Skip to content
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

withConverter doesn't work when used with CollectionReference #2601

Closed
maletor opened this issue Feb 6, 2020 · 8 comments
Closed

withConverter doesn't work when used with CollectionReference #2601

maletor opened this issue Feb 6, 2020 · 8 comments
Assignees

Comments

@maletor
Copy link
maletor commented Feb 6, 2020

@thebrianchen, it seems types don't work correctly with CollectionReference.

 const posts = await store
  .collection("posts")
  .withConverter({ toFirestore, fromFirestore })
  .get();

posts.forEach(doSomething);

The forEach callback takes a QueryDocumentSnapshot (not a DocumentSnapshot) and there's no way to get the T on that object. It's only on DocumentSnapshot.

Is this by design, can we fix this

@google-oss-bot
Copy link
Contributor

I found a few problems with this issue:

  • I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.
  • This issue does not seem to follow the issue template. Make sure you provide all the required information.

@thebrianchen
Copy link

@maletor Thanks for writing in!

The forEach callback takes a QueryDocumentSnapshot because the documents are guaranteed to exist since they are coming from a QuerySnapshot. QueryDocumentSnapshot has support for generics, and you should be able to access T.

For example, the following code snippet using the Post class from the documentation should be valid:

 const posts = db
  .collection('posts')
  .withConverter(PostConverter)
  .get();

posts.forEach((snapshot: QueryDocumentSnapshot<Post>) => {
  const post: Post = snapshot.data();
  ...
});

@maletor
Copy link
Author
maletor commented Feb 6, 2020

I would think so as well. But...

Screen Shot 2020-02-06 at 11 34 50 AM

Either way, the generic shouldn't be necessary to provide. It should already be there from withConverter returning a type Post like with DocumentReference.

@thebrianchen
Copy link
thebrianchen commented Feb 6, 2020

Can you provide a minimal repro so I can see what's going on? It seems to work fine for me in a local test I wrote with that snippet I posted above, even without the generic type specification.

@maletor
Copy link
Author
maletor commented Feb 6, 2020

@thebrianchen
Copy link
thebrianchen commented Feb 6, 2020

@maletor Thanks for the repro! You are using the admin SDK, which is located at https://github.com/googleapis/nodejs-firestore/. I was able to reproduce this on the admin Node SDK and I'll make a bug fix for it soon.

The root cause is that get() return QuerySnaphot instead of QuerySnapshot<T>, which would remove your typing.

@maletor
Copy link
Author
maletor commented Feb 6, 2020

@thebrianchen, doesn't the admin SDK simply use the firestore SDK? It's not doing anything different. It's the same type. Correct me if I'm wrong.

Addendum: Yup, I'm wrong.

https://github.com/googleapis/nodejs-firestore/blob/94ddc897400cafe5a1ee16f3ad0d285411bdd0b2/dev/test/query.ts#L658

@thebrianchen
Copy link

Marking as closed. The fix for the admin SDK should go out with the next release.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants