我有两个架构,FinishedGame
和PlayingGame
。两种模式都是相同的。当我关闭游戏时,想要将其复制PlayingGame
到FinishedGame
集合中,然后将其删除PlayingGame
。现在的代码似乎没有引发错误,但也没有添加任何内容FinishedGame
。当我打开shell运行时,show collections
我只会看到playinggames
和system.indexes
。任何帮助是极大的赞赏。
这是我要关闭游戏时运行的代码:
console.log('finding ', gameId);
PlayingGame.findById(gameId, function(err, game) {
if (err) {
console.log(err);
throw 'Problem finding game when closing';
}
console.log(game);
// if game found, move PlayingGame to FinishedGame emit game closed to room
if (game) {
console.log('Saving game to finished games');
var finishedGame = new FinishedGame(game);
finishedGame.save(function(err) {
if (err) throw 'Problem saving finished game when moving playing game to finished game';
console.log('Successfuly saved to finish game');
game.remove(function(err) {
if (err) throw 'Problem removing from playing games';
socket.leave('game:' + gameId);
// send message to room that the game has been closed
io.to('game:' + gameId).emit('game closed');
});
});
}
});
你需要调用toObject()
上game
它传递给之前FinishedGame
的构造,因为它是期待一个简单的JS对象,而不是猫鼬文档实例。
因此,将该行更改为:
var finishedGame = new FinishedGame(game.toObject());
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句