Closed welkinwong closed 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.
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
Hmm, I wonder if I need to do something different for server side. Thanks for the report! I'll look in to it.
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.
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:
checkCursor
reports warnings on the server. Not that big a deal, because it's only in development mode.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: https://github.com/edemaine/solid-meteor-data/commit/7a7019885116bb1859cad3f1efd2750e145f09b3
server alert some warning