Meteor:Meteor.call()from observe回调函数不执行 [英] Meteor: Meteor.call() from within observe callback does not execute
问题描述
是否有可能从Meteor中的观察 c>回调中调用服务器方法?
一个重现问题的示例, myCursor.observe()的回调中调用
不执行。当从obser回调中调用时, Meteor.call()
Meteor.method
本身也不回调带有错误,它只是返回 Undefined
停止忽略我, Meteor.call()
:)任何帮助非常感谢! >
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);
}
}
},
remove:function(doc){
console.log(removed+ doc.text);
Meteor.call('aMethod',doc.text,函数(e,r){
if(e){
console.log(error from server:+ e);
} else {
console.log来自服务器的响应:+ r);
}
});
}
});
});
Template.test.items = function(){
return items.find();
}
Template.test.events({
'click #add':function(){
items.insert({text:Timestamp:+ 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);
returnGot it!
}
});
}
observe.html
$ b
< head>
< title> observe< / title>
< / head>
< body>
{{> test}}
< / body>
< template name =test>
< button id =add>添加项< / button>
< button id =remove>删除项< / button>
< ol>
{{#each items}}
< li> {{text}}< / li>
{{/ each}}
< / ol>
< / template>
这可能是一个已知问题,确定,因为我没有自己尝试,但它似乎可能有一个解决方法(请参阅 https://github.com / meteor / meteor / issues / 907 )
将 Meteor.call
添加到即时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(server from server:+ r);
}
});
},0);
}
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);
}
这篇关于Meteor:Meteor.call()from observe回调函数不执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!