来自Mongoose子文档的方法不起作用

时间:2014-03-15 17:28:14

标签: node.js mongodb mongoose

鉴于我所处的具体情况,附属于子文档的方法尚未找到。当我使用调试器语句停止代码并访问“game.players [0] .token() - 附加到playerStatus子模式的方法时,我得到:

" TypeError:对象#没有方法'转'"。

这是代码: PlayerStatus.js

// Player Status model
var Card = require('./card.js');

var mongoose = require('mongoose'),
    Schema   = mongoose.Schema,
    ObjectId = Schema.ObjectId;

var playerStatusSchema = new Schema({
  // user:    Object,
  user_id:   String,
  firstName: String,
  game_id:   String,
  deck:      [Card.getSchema() ],
  hand:      [Card.getSchema() ],
  discard:   [Card.getSchema() ],
  turnTokens: Array,
  buys:       Number,
  actions:    Number,

});

playerStatusSchema.methods = {

  turn: function() {
    return this.turnTokens[this.turnTokens.length-1];
  },

...
playerStatusSchema.statics = {
  getSchema: function() {
    return this.playerStatusSchema;
  }
};
module.exports = mongoose.model('PlayerStatus', playerStatusSchema);

我访问子文档的代码部分'方法:

var PlayerStatus = require('../models/playerStatus.js');
var Game         = require('../models/game.js');
var User         = require('../models/user.js');
...

exports.purchase = function(req, res) {
this.gameId          = req.body.gameId;
this.userId        = req.body.userId;
this.card            = req.body.card;
this.treasureCardIds = req.body.treasureCardIds;

Game.findById(this.gameId, _und.bind(function(err, game) {
  if (err) return err;
  debugger;
  game.players[0].turn();

有趣的是,在Game文档中,我可以调用播放器子文档对象并使用这些播放器的方法就好了。但在这种情况下,我在控制器类中,而不是文档的父类。

有谁知道我做错了什么?在此先感谢您的帮助。

0 个答案:

没有答案