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

how to use ssr(React Router SSR ) #344

Closed
welkinwong opened this issue Dec 8, 2021 · 5 comments
Closed

how to use ssr(React Router SSR ) #344

welkinwong opened this issue Dec 8, 2021 · 5 comments
Labels

Comments

@welkinwong
Copy link

server alert some warning

Warning: useFind requires an instance of Mongo.Cursor. Make sure you do NOT call .fetch() on your cursor.
@CaptainN
Copy link
Collaborator

CaptainN commented Dec 8, 2021

Can you post your example code? That error means that your factory method does not return a Cursor, which is required for useFind to work.

@welkinwong
Copy link
Author

Can you post your example code? That error means that your factory method does not return a Cursor, which is required for useFind to work.

ssr package: meteor/communitypackages:fast-render

my code:

const docIsReady  = useSubscribe('metas.docInfo', { docId });
const docMeta = useFind(() => MetasCollection.find({ _id: docId }), [docId]);

console.log(docMeta);

the code will be running on the client and server

client console:

[]
[]
[object]

server console:

W20211209-19:22:41.014(8)? (STDERR) Warning: useFind requires an instance of Mongo.Cursor. Make sure you do NOT call .fetch() on your cursor.
I20211209-19:22:41.014(8)? null

@CaptainN
Copy link
Collaborator

CaptainN commented Dec 9, 2021

Hmm, I wonder if I need to do something different for server side. Thanks for the report! I'll look in to it.

@CaptainN CaptainN added the bug label Dec 9, 2021
@CaptainN
Copy link
Collaborator

A quick audit of this shows that it should be fine. I wonder if fast render is preventing a Cursor from being returned. I'll have to look more closely in to what's going on there.

@edemaine
Copy link
Contributor

I found the bug here. On the client, find returns a Mongo.Cursor (defined in minimongo/cursor.js and exported in mongo/collection.js). On the server, however, find instead returns an instance of a private Cursor type defined in mongo/mongo_driver.js). Sadly, the latter type is not exported, but it does have distinctive fields _mongo and _cursorDescription.

This causes two bugs in useFind in an SSR context:

  • As reported above, checkCursor reports warnings on the server. Not that big a deal, because it's only in development mode.
  • More crucially, useFindServer never returns any results, as cursor instanceof Mongo.Cursor is always false. I'd suggest changing the check to just cursor ? cursor.fetch() : null. If the cursor isn't null but isn't a cursor, the user will have already gotten a warning, which will help track down the resulting error.

I just ran into the same issue and tracked down the bug in the context of my port of this library from React to SolidJS, solid-meteor-data. Here's the commit that fixed the issues for me, and should apply equally well to react-meteor-data: edemaine/solid-meteor-data@7a70198

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

No branches or pull requests

3 participants