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

Meteor.autorun is not "autorunning" on Collection changes #471

Closed
speigg opened this issue Nov 9, 2012 · 8 comments
Closed

Meteor.autorun is not "autorunning" on Collection changes #471

speigg opened this issue Nov 9, 2012 · 8 comments

Comments

@speigg
Copy link

speigg commented Nov 9, 2012

Is there any reason why inserts to the "test" collection bellow do not trigger an autorun in the reactive context?

My code:

Test = new Meteor.Collection("test");

if (Meteor.isServer) {

    Test.remove({});    

    Meteor.setInterval(function() {
        console.log("Inserting Test");
        Test.insert({test: 1}); 
    }, 1000);

    Meteor.autorun(function() {

        // This section only runs once?!

        var tests = Test.find({}); 

        console.log("(" + tests.count() + ") Tests:");
        tests.forEach(function (t) {
            console.log(t);
        });

    });

}

My output:

(0) Tests:   // this came from the Meteor.autorun, but only runs the first time
Inserting Test
Inserting Test
Inserting Test
Inserting Test
Inserting Test
... etc
@speigg
Copy link
Author

speigg commented Nov 9, 2012

BTW, I can confirm that the Test objects are being successfully inserted into the collection. The problem seems to be with the reactive context never being invalidated when the Test collection changes.

According to the documentation, "When called in a reactive context, forEach registers dependencies on the matching documents." Meteor.autorun's job is to make a reactive context, and I've used it successfully before, so it seems that forEach is not honoring its contract of registering a dependency.

@glasser
Copy link
Contributor

glasser commented Nov 9, 2012

If you look at the docs for the deps system, you'll see that it's
documented as client-only. While the basic building blocks of deps
(Context, autorun, etc) will work on the server, Meteor doesn't provide any
reactive data sources on the server. Specifically, it's the client-side
local cache ("minimongo") that is a reactive data source; server-side
collections are not directly reactive. (You can use cursor.observe on the
server to do something similar though.)

On Fri, Nov 9, 2012 at 1:57 PM, speigg notifications@github.com wrote:

BTW, I can confirm that the Test objects are being successfully inserted
into the Collection. The problem seems to be with the reactive context
never being invalidated when the Test collection changes.

According to the documentation, "When called in a reactive context,
forEach registers dependencies on the matching documents." Meteor.autorun's
job is to make a reactive context, and I've used it successfully before, so
it seems that forEach is not honoring its contract of registering a
dependency.


Reply to this email directly or view it on GitHubhttps://github.com//issues/471#issuecomment-10246061.

@speigg
Copy link
Author

speigg commented Nov 9, 2012

Ah, that explains everything. Thanks so much.

@speigg speigg closed this as completed Nov 9, 2012
@skeetmtp
Copy link

I stucked on the same probleme, before finding the reason here. May be it could help people to add a little message in the server console when someone is trying to call Deps function on the server ?

@dmacd
Copy link

dmacd commented Apr 23, 2013

Yes. Just wasted about two hours on this exact issue with nairy a whisper that Deps.autorun was being used it ways not intended.

@mizzao
Copy link
Contributor

mizzao commented Jan 15, 2014

Took me several months of Meteor before I bumped into this too. Using Deps.autorun on the server should probably result in an error message.

Then again, declarative programming is very clean and less error prone. Couldn't we entertain the idea of having deps on the server?

@matb33
Copy link

matb33 commented Jan 17, 2014

Just ran into this today. Would also love to be able to use Deps to its full capacity on the server (such as in a setInterval)

@hexsprite
Copy link
Contributor

+1 for Deps.autorun on server or at least a WARNING. I just spent the last hour trying to figure out why it wasn't re-running when I thought it should.

EDIT: re-reading apparently Deps.autorun does exist, its just missing reactive data sources. Either way it's a bit of a wart of the iso JS experience.

StorytellerCZ pushed a commit that referenced this issue Sep 18, 2021
…t-6.10.3

Update eslint-plugin-react to version 6.10.3 🚀
StorytellerCZ pushed a commit that referenced this issue Oct 1, 2021
* Add link to PascalCase in c2 wiki

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

No branches or pull requests

7 participants