我对 Node.js 还很陌生,我正在启动一个新的 Web 应用程序只是为了学习。 所以,我正在使用 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
?
感谢任何帮助!
最佳答案
对于测试用例,我认为您可以使用查询邀请中的数据和来自聚会的填充数据。
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
})
关于node.js - 使用 mongoose 为 Node.js 建模,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19260858/