简体   繁体   中英

Meteor.call not working

I had this code working before I wanted to change the client side collection find/insert methods to server side. I removed insecure and autopublish from my meteor project, and changed my code to what it is below.

My angular Code in client/controllers/item-controller.js

    angular.module('prototype').controller('ItemController', ['Config','$window','$meteor', function(Config, $window, $meteor) {

    this.items = function(){
        Meteor.call('getAllItems', function(err, res){
            alert("error: " +err + " res: " + res );
            return res;
        });
    }

My item-collection codee in server/item-collection-methods.js

Meteor.methods({
    getAllItems : function(){
        console.log("i got here")
        return Items.find();
    }
});

My main file in lib/app.js

Items = new Mongo.Collection("Items");

Before I had 15 items showing, now none of them show.

when I copy my Meteor.call function into the chrome console, all I get back is undefined .

I have a feeling it either has to do with the project structure, or the fact that autopublish and insecure are removed. Any advice would be helpful.

EDIT:

I did get something in my server console

I20150629-00:54:54.402(-4)? Internal exception while processing message { msg: '
method', method: 'getAllItems', params: [], id: '2' } Maximum call stack si
ze exceeded undefined

Meteor data transmission works with a publish/subscribe system. This system is able to replicate part of or all the data that is stored in your MongoDB (server) to the client in an in-memory DB (MiniMongo). Autopublish was publishing everything on the client, as you removed it there is nothing in your Items collection anymore.

In order to publish some data to the client you have to declare a publication on the server side:

Meteor.publish('allItems', function () { 
  //collection to publish
  return Items.find({});
});

And subscribe on the client (either in the router or in a template):

Meteor.subscribe('allItems');

To learn more about this system you can read the official docs .

Concerning your method "getAllItems", you cannot directly send a cursor ( Items.find() ) on your data, that is why you are getting the error message "Maximum call stack size exceeded". But you could send an array of these data by returning Items.find().fetch() . Also the call to a Meteor method is asynchronous, so you have to use the callback ( more on Meteor methods ) Please note that by sending data over a method (which is perfectly acceptable) you lose the reactivity offered by the publish/subscribe system.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM