Meteor: collection.find (). Fetch () inside Deps.autorun doesn't work

I want to create a reactive collection query on a meteorite server and process the results whenever there are changes.

Here is my code:

if (Meteor.isServer) {
  Meteor.startup(function(){
    Deps.autorun(function(){
      var items=new Meteor.Collection('name').find().fetch();
      // ... process the items ...
    });
  });
}

      

(Actually, for this test, the entire project consists of just the above code in a single .js file). Running this program with meteor

raises an error:

/home/yaakov/Bug/.meteor/local/build/programs/server/boot.js:198
}).run();
   ^
Error: Can't call yield in a noYieldsAllowed block!
    at Function.Fiber.yield (packages/meteor/fiber_helpers.js:11)
    at Function.wait (home/yaakov/.meteor/tools/cef2bcd356/lib/node_modules/fibers/future.js:111:14)
    at Object.Future.wait (/home/yaakov/.meteor/tools/cef2bcd356/lib/node_modules/fibers/future.js:325:10)
    at new MongoConnection (packages/mongo-livedata/mongo_driver.js:196)
    at new MongoInternals.RemoteCollectionDriver (packages/mongo-livedata/remote_collection_driver.js:4)
    at Object.<anonymous> (packages/mongo-livedata/remote_collection_driver.js:44)
    at Object.defaultRemoteCollectionDriver (packages/underscore/underscore.js:750)
    at new Meteor.Collection (packages/mongo-livedata/collection.js:72)
    at app/Bug.js:4:17
    at packages/deps/deps.js:47

      

What I did wrong? How to create a reactive collection request on a meteorite server?

+3


source to share


1 answer


The package Deps

only works on the client side, so you cannot use Deps.autorun

on the server.

To use reactive query on the server, use observe

instead:



var items=collection.find().observe({
    added: function (document) {
        // ...
    },
    changed: function (newDocument, oldDocument) {
        // ...
    },
    removed: function (oldDocument) {
        // ...
    }
});

      

+6


source







All Articles