2016-05-13 74 views
0

我试图访问集合名称的帖子来检查当前用户是否已经喜欢。如果喜欢,然后显示不同颜色的喜欢的按钮。 问题是该函数被称为两次。在使用流星调用时多次访问流星中的函数

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

上述函数调用posts.isLiked如下 -

'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倍。 任何帮助,将不胜感激。

回答

0

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

下面

是什么,我想请教大家:

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被调用两次。

+0

isLiked被称为两次,因此流星调用也被称为两次。 –

+0

那么,现在我们该如何帮忙?我们没有你的代码,你在做什么?为什么叫两次? –

+0

在Html部分,我使用{{#if isLiked}} –