Meteor mongo what is fetch()?
Asked Answered
A

2

5

I recently started learning Meteor, so i learned how to work with Mongo via Meteor but there is something i don't understand, in the web I often see examples like: SomeCollection.find().fetch() , I cannot see what is fetch() is good for?

In the docs, they say:

When called from a reactive computation, fetch registers dependencies on the matching documents

Does that mean that when the data in SomeCollection changes they will be changed in the view?

Isn't the cursor returned by find() is reactive by default (the changes on the data on the cursor will automatically be visible in the view)?

Can anyone clear this ambiguity for me ?

Arrester answered 22/6, 2017 at 15:17 Comment(0)
I
2

I see the documentation says:

Return all matching documents as an Array.

Collection.find() returns a cursor containing the matching documents to that collection, and Collection.find().fetch() returns an array(containing those documents) to that cursor.

Interplead answered 22/6, 2017 at 18:3 Comment(0)
S
4

The cursor from someCollection.find() is good enough as the return value of a Blaze template helper. Blaze knows how to iterate cursors and arrays. And yes, when the cursor changes, the template view will be automatically updated accordingly.

But in some situations you want to process the result of your collection query with some other algorithm / library that understands only an array, in which case you would simply fetch() the cursor before processing it. If you need this process to be re-run when the cursor changes, simply wrap it with this.autorun / Tracker.autorun so that it becomes a "reactive computation" the docs talk about.

Strachan answered 22/6, 2017 at 19:18 Comment(0)
I
2

I see the documentation says:

Return all matching documents as an Array.

Collection.find() returns a cursor containing the matching documents to that collection, and Collection.find().fetch() returns an array(containing those documents) to that cursor.

Interplead answered 22/6, 2017 at 18:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.