meteor / react-packages

Meteor packages for a great React developer experience
http://guide.meteor.com/react.html
Other
571 stars 157 forks source link

how to use ssr(React Router SSR ) #344

Closed welkinwong closed 2 years ago

welkinwong commented 2 years ago

server alert some warning

Warning: useFind requires an instance of Mongo.Cursor. Make sure you do NOT call .fetch() on your cursor.
CaptainN commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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

CaptainN commented 2 years ago

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 commented 2 years ago

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:

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: https://github.com/edemaine/solid-meteor-data/commit/7a7019885116bb1859cad3f1efd2750e145f09b3