Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meteor: Meteor.call() from within observe callback does not execute

Is there any possibility of calling a server method from within a observe callback in Meteor?

I put together an example that reproduces the issue, that a Meteor.call() called from within a callback of myCursor.observe() does not execute. When called from within the observe callback, the Meteor.method itself also does not callback with an error, it just returns Undefined.

Stop ignoring me, Meteor.call() :) Any help is very appreciated!

observe.js

items=new Meteor.Collection("Items");

if (Meteor.isClient) {
    Meteor.subscribe("Items");
    Meteor.startup(function(){
        itemsCursor=items.find();
        itemsHandle=itemsCursor.observe({
            added : function(doc){
                console.log("added "+doc.text);
                Meteor.call('aMethod',doc.text,function(e,r){
                    if(e){
                        console.log("error from server: "+e);
                    }else{
                        console.log("response from server: "+r);
                    }
                });
            },
            removed : function(doc){
                console.log("removed "+doc.text);
                Meteor.call('aMethod',doc.text,function(e,r){
                    if(e){
                        console.log("error from server: "+e);
                    }else{
                        console.log("response from server: "+r);
                    }
                });
            }
        });
    });

    Template.test.items=function(){
        return items.find();
    }
    Template.test.events({
        'click #add':function(){
            items.insert({"text":"Timestamp: "+(new Date().getTime())});
        },
        'click #remove':function(){
            items.remove(items.findOne()._id);
        }
    });
}

if (Meteor.isServer) {
    Meteor.publish("Items",function(){
        return items.find();
    });
    items.allow({
        insert : function(userId,doc){
            return true;
        },
        update : function(userId,doc){
            return true;
        },
        remove : function(userId,doc){
            return true;
        }
    });
    Meteor.methods({
        aMethod:function(text){
            console.log("Got it! "+text);
            return "Got it! "+text;
        }
    });
}

observe.html

<head>
  <title>observe</title>
</head>

<body>
  {{> test}}
</body>

<template name="test">
  <button id="add">add item</button>
  <button id="remove">remove item</button>
    <ol>
        {{#each items}}
        <li>{{text}}</li>
        {{/each}}
    </ol>
</template>
like image 481
Moritz Walter Avatar asked Mar 22 '23 12:03

Moritz Walter


1 Answers

This might be a known issue, i'm not sure since I've not tried it myself, but it looks like there might be a workaround (see https://github.com/meteor/meteor/issues/907)

Add your Meteor.call into an instantaneous setTimeout callback:

added: function(doc) {
    console.log("added "+doc.text);
    setTimeout(function() {
        Meteor.call('aMethod',doc.text,function(e,r){
            if(e){
                console.log("error from server: "+e);
            }else{
                console.log("response from server: "+r);
            }
        });
    },0);
}
like image 183
Tarang Avatar answered Apr 24 '23 02:04

Tarang