2016-04-03 76 views
1

我有兩個模式,FinishedGamePlayingGame。兩種模式都是相同的。當我關閉遊戲時,我想將PlayingGame複製到FinishedGame集合,然後刪除PlayingGame。現在的代碼似乎不會引發錯誤,但它也不會添加任何內容到FinishedGame。當我打開外殼運行show collections我只看到playinggamessystem.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'); 
      }); 
     }); 
    } 

}); 
+0

也許'遊戲'不是由id找到的?你是否看到'在遊戲機中保存遊戲到完成的遊戲信息? – alexmac

+0

這工作@JohnnyHK如果你發佈的答案我可以選擇它 –

回答

0

你需要將它傳遞給FinishedGame構造,因爲它是期待一個簡單的JS對象,而不是貓鼬之前呼籲gametoObject() doc實例。

因此改變該行:

var finishedGame = new FinishedGame(game.toObject()); 
0

是的,我同意佐尼的答案。你也可以使用 var gameClone = JSON.parse(JSON.stringify(game)); var finishedGame = new FinishedGame(gameClone);