1
我有一種方法,用戶加入現有遊戲或創建新遊戲。此外,相應的ID的ID存儲在一個對象,並返回作爲每下面:流星:如何確保在進入Router.go之前從方法更新新的會話變量?
Meteor.methods({
newGame: function(gameQuestions) {
var user = Meteor.user();
var gameWaiting = GameCollection.findOne({players: {$size: 1}, active: false, current: true});
var result = {};
if (!gameWaiting) {
var gameId = GameCollection.insert
//.....
result.id = gameId;
} else {
if (_.contains(gameWaiting.players, user._id))
//.....
result.id = gameWaiting._id;
} else {
//......
result.id = gameWaiting._id;
}
}
return result
},
在客戶端側I具有其中用戶點擊新的遊戲,並把由該方法返回的ID的按鈕並將其放入會話變量中。從那裏的路由器所提供的ID:
Template.loggedIn.events({
"click #newGame": function() {
var gameQuestions = Questions.find().fetch();
Meteor.call('newGame', gameQuestions, function(err, data) {
if (err)
console.log(err);
Session.set('uniqueId', data);
console.log(data)
});
var gameId = Session.get('uniqueId').id;
console.log(gameId);
Router.go('gamePage', {_id: gameId});
}
});
然而,會話變量保存爲舊的會話變量和路由器然後去到錯誤的地址。下面是從上述控制檯:
creating a new game, none available
yKrSeeAfMh4ppXWJP
Object {id: "AKXYbWcdJWfaXfo8C"}
這是地址和實際遊戲ID從流星方法返回
http://localhost:3000/games/yKrSeeAfMh4ppXWJP
Current Game ID: AKXYbWcdJWfaXfo8C
如何確保VAR遊戲ID與新的ID更新前進行路線?