1

我对 Node.js 很陌生,我正在开始一个新的网络应用程序只是为了学习。所以,我正在使用 Mongoose.js,这是我的模型:

派对模式:

var PartySchema = new Schema({
  created: {
    type: Date,
    default: Date.now
  },
  title: {
    type: String,
    default: '',
    trim: true
  },
  createdBy: {
    type: Schema.ObjectId,
    ref: 'User'
  },
  invitations: [{
    type: Schema.ObjectId,
    ref: 'Invitation'
  }]
});

邀请模式:

var InvitationSchema = new Schema({
  party: {
    type: Schema.ObjectId,
    ref: 'Party'
  }
});

好的,所以我写了一个这样的测试用例,假设另一个变量(party)已正确初始化:

invitation = new Invitation({
  party: party,
  content: 'come to this awesome party'
});

当我将此变量邀请传递给这样的方法时:

return Party.sendInvitation(invitation, function(err, invitation) {
  should.exist(invitation);
  should.not.exist(err);
  done();
});

在 sendInvitation 方法中访问invitation.party 是这样的:

invitation.party[525439f929cf32be02000003]

看来,我无法浏览invitation.party. 我怎样才能做到这一点invitation.party.title

我很感激任何帮助!

4

1 回答 1

0

对于测试用例,我认为您可以使用来自查询邀请的数据和来自派对的填充数据。

Invitation
.findById(id)
.populate('title')
.exec(function (err, invitation) {
  if (!err) 
     return invitation; //this invitation data must be contained party with title. invitation.party.title
})
于 2013-12-05T10:14:49.400 回答