我在server /functions.js文件中有以下内容:
Meteor.methods({
checkIfVoted: function (postId) {
if (postId) {
...
if (condition is met) {
return true;
} else {
return false;
}
}
}
});
Run Code Online (Sandbox Code Playgroud)
然后在客户端 /showPost.js中的以下内容:
Template.showPost.helpers({
alreadyVotedByUser: function () {
var answer = false;
if(this) {
Meteor.call("checkIfVoted", this._id, function(err, response) {
if (response) { console.log(response); }
answer = response;
});
}
console.log(answer);
return answer;
}
});
Run Code Online (Sandbox Code Playgroud)
当我在执行console.log响应时,我在条件满足时得到值为true,但是answer变量不接受它,并且仍然显示为其值为false.
(我知道我将Meteor方法放在服务器目录中而不是在公共目录中,以便在客户端和服务器之间共享以提供延迟补偿)
如果有人可以帮助我,我们将非常感谢.
Dav*_*don 13
在客户端上,Meteor.call它是异步的 - 它返回undefined并且它的返回值只能通过回调访问.另一方面,助手同步执行.有关如何从帮助程序调用方法的信息,请参阅此问题的答案.这是一个快速的解决方案:
$ meteor add simple:reactive-method
Run Code Online (Sandbox Code Playgroud)
Template.showPost.helpers({
alreadyVotedByUser: function () {
return ReactiveMethod.call('checkIfVoted', this._id);
}
});
Run Code Online (Sandbox Code Playgroud)
这个包的问题在于它可以鼓励不良行为,但对于不改变状态的方法调用应该没问题.
还看到在助手的部分这篇文章.
| 归档时间: |
|
| 查看次数: |
2893 次 |
| 最近记录: |