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>
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With