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>
</body>
<template name="test">
<button id="add">add item</button>
<button id="remove">remove item</button>
<ol>
<li>
</ol>
</template>
No comments:
Post a Comment