使用流星调用时,可以多次访问流星中的某个函数

Multiple access of a function in meteor while using meteor call

本文关键字:流星 访问 函数 调用      更新时间:2023-09-26

我正在尝试访问集合名称帖子,以检查当前用户是否已经喜欢。如果喜欢,则显示不同颜色的喜欢按钮。问题是函数被调用了两次。

  isLiked: function() {
let self = this;
console.log();
Meteor.call('posts.isLiked', self._id, (error, result) => {
    console.log(result);
    return result;
}); 
}

上面的函数调用posts.is如下所示-

  'posts.isLiked': (_id) => {
    check(_id, String);
    if (!Meteor.user()) {
      throw new Meteor.Error(401, 'You need to be signed in to continue');
    }
    if (!_id) {
      throw new Meteor.Error(422, '_id should not be blank');
    }
  return (Posts.find( { _id: _id , already_voted: { "$in" : [Meteor.userId()]} }).count() == 1);
}

控制台显示输出2次。如有任何帮助,我们将不胜感激。

我认为,因为您使用的是ES6函数,您可能会遇到问题,调试应用程序是唯一的解决方案,但您可以使用基本函数调用而不是ES6规范吗?

以下是我想问你的问题:

  isLiked: function() {
        let self = this;
        console.log();
        Meteor.call('posts.isLiked', self._id, function(error, result) {
           console.log(result);
           return result;
        }); 
  }
  'posts.isLiked': function(_id){
    check(_id, String);
    if (!Meteor.user()) {
      throw new Meteor.Error(401, 'You need to be signed in to continue');
    }
    if (!_id) {
      throw new Meteor.Error(422, '_id should not be blank');
    }
  return (Posts.find( { _id: _id , already_voted: { "$in" : [Meteor.userId()]} }).count() == 1);
}

还要在isLiked的第一行放一个控制台日志,这样你就会知道Meteor.call不会被调用两次,但可能是isLiked被调用了两次。