调用Meteor.methods后无法将结果传递给路由器
我在使用Meteor时遇到了一个错误。我调用一个Method.Method调用Meteor.methods后无法将结果传递给路由器,meteor,iron-router,Meteor,Iron Router,我在使用Meteor时遇到了一个错误。我调用一个Method.Method Template.WelcomeTemplate.events({ 'click #btn-findgame': function(e) { e.preventDefault(); console.log('clicked find game button'); Meteor.call('allocateGame', function(error, id) { if (erro
Template.WelcomeTemplate.events({
'click #btn-findgame': function(e) {
e.preventDefault();
console.log('clicked find game button');
Meteor.call('allocateGame', function(error, id) {
if (error) {
alert(error.reason);
} if (id) {
Router.go('gameRoom', {_id: id})
}
})
}
})
使用我的方法,我检查是否有可用的房间,在房间未加入时创建一个房间。并返回这个房间的ID
Meteor.methods({
allocateGame: function () {
console.log('allocateGame method called')
var user = Meteor.user();
// find game where one player is in the room
var gameWaiting = Games.findOne({players: {$size: 1}})
if (!gameWaiting) {
console.log('no game available, create a new one');
var newGameId = Games.insert({players: [user._id], active: false, finished: false});
GameDetails.insert({gameId: newGameId, gameData: []});
return newGameId
} else {
if (_.contains(gameWaiting.players, user._id)) {
console.log('Cannot play against yourself sir')
} else {
console.log('Joining game');
Games.update({_id: gameWaiting._id}, {
$set: {active: true},
$push: {players: user._id}
});
return gameWaiting._id;
}
};
}
})
和我的路由器:
Router.map(function () {
this.route('welcome', {
path: '/',
controller: WelcomeController})
this.route('gameRoom', {
path: '/game/_:id'
})
});
我收到的错误是:
Exception in delivering result of invoking 'allocateGame': TypeError: Cannot read property 'charAt' of null
at Object.IronLocation.set (http://localhost:3000/packages/iron-router.js?e9fac8016598ea034d4f30de5f0d356a9a24b6c5:1293:12)
事实上,如果我不返回ID,路由将继续正常进行。但是,当我在WelcomeTemplate中返回ID时,将发生错误
编辑:
即使我的MongoDB正在更新,我的MiniMongo DB也是空的。同步一定有问题。你知道往哪里看吗?在路线中,你将路径设置为
'/game/\uuID'
,也就是说,一个名为id
的参数。在调用Router.go
时,您传递了一个名为\u id
的参数
我不知道这是否解决了您的问题,但这是一个错误。考虑到我花了多少时间来解决这个问题,这有点尴尬。该错误是由于my routers.js中的错误而创建的
this.route('gameRoom', {
path: '/game/_:id'
})
应该是:
this.route('gameRoom', {
path: '/game/:_id'
})
快乐编码 注意:路径应该是
'/game/:\u id'
。谢谢,但这不是问题,这是一些神奇的铁路由器可以处理的。看起来我的miniMongo datbase没有同步。谢谢Hubert OG。我忽略了你的答案,它确实是“/game/:\u id”而不是“/game/\u id”。在我发现这个问题之前,我几乎重写了整个应用程序:D