我有两个模式,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对象而不是Mongoose doc实例。
因此,将该行更改为:
var finishedGame = new FinishedGame(game.toObject());