2015-06-06 37 views
0

下面有一個把手幫手,它顯示了選定的玩家威脅。它的工作原理。在方法中使用Meteor.users.findOne If語句

Handlebars.registerHelper('sthreat', function() { 
    var sthreat = Meteor.users.findOne({_id: Session.get('selectedPlayer')}, {threat: 1}); 
    return sthreat; 
}); 

但是下面的按鈕(客戶端)和方法(在服務器上)的假設,以檢查球員繼續前5個或更多的威脅,但他們沒有工作。

'click input.increment': function(){ 
    var selectedThreat = Meteor.users.findOne({_id: Session.get('selectedPlayer')}, {threat: 1}); 
    Meteor.call('incclick',selectedThreat); 
}, 

incclick: function (selectedThreat) { 
    if(selectedThreat <= 4) { 
    } else { 
    Meteor.users.update({_id: Session.get('selectedPlayer')}, {$inc: {'threat': -5}}); 
    Meteor.users.update({_id: this.userId}, {$inc: {'threat': 5}}); 
    } 
}, 

我想如果我能得到值顯示在助手中,我應該能夠在方程中使用它。有什麼我在這裏失蹤?

回答

1

Meteor.users.findOne將返回一個文件。您作爲選項通過了{threat: 1},但它們將被忽略,因爲它們都不是有效的。諸如sort,fieldlimit之類的選項可以工作。

我認爲你正在尋找文檔的屬性。 Meteor.users.findOne的結果會是這樣的

{ 
    _id : .. 
    threat: 3, 
    ... 
} 

所以,你可以通過屬性的方法,而不是整個文檔:

Meteor.call('incclick',selectedThreat.threat); 
+0

當我做了這個樣子了一個錯誤。錯誤調用方法'incclick':內部服務器錯誤[500] –

+1

@JonPotts這是由於您的方法調用中無關的錯誤。你應該檢查你的服務器端控制檯的細節。我懷疑這是'Session',它不受服務器端支持。 – Akshat