node.js - 使用 mongoose 为 Node.js 建模

标签 node.js mongoose

我对 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/

相关文章:

node.js - SailsJs 无效模块错误

javascript - 将对象写入文件时的 Node JS [对象映射]

javascript - 在运行nodejs的服务器上调用new Date()时,日期返回服务器启动时的日期

node.js - 如何在前一个函数完成之前阻止一个函数? Node

node.js - 如何在 Mongoose 中查找文档时剪切文本?

javascript - NodeJs、AngularJs、Mongoose 在 Function.keys( native )的非对象上调用错误 : TypeError: Object. 键

javascript - 将多个 Promise 传递到 Node JS 渲染中

javascript - 更新 Mongoose 模型内的数组

node.js - 替换文档不得包含原子运算符

node.js - MongoParseError : Invalid message size: 1347703880, 最大允许值:67108864