在节点/ mongoose应用程序中使用子文档

时间:2018-05-18 22:33:53

标签: node.js mongodb express mongoose

我在两个文件中定义了以下架构。

faultreport.js:

var mongoose = require('mongoose');

var Schema = mongoose.Schema;

var FaultReportSchema = new Schema(
  {
    reporter: {type: String, required: true, max: 128},
    comment: [{type: Schema.ObjectId, ref: 'Comment'}],
    status: {type: String, default: 'Reported', max: 64},
    datetime: {type: Date, required: true},
  }
);

module.exports = mongoose.model('FaultReport', FaultReportSchema);

comment.js

var mongoose = require('mongoose');

var Schema = mongoose.Schema;

var CommentSchema = new Schema(
  {
    commenter: {type: String, required: true, max: 128},
    comment: {type: String, required: true, max: 1024},
    datetime: {type: Date, required: true},
  }
);

module.exports = mongoose.model('Comment', CommentSchema);

我的想法是每个FaultReport都与一个关于创建的注释相关联,但是稍后可以添加更多注释。我想构建一个可用于列出所有FaultReports的快速路由,包括相关注释中的注释字符串。

我试图这样做。

router.get('/list_all', function(req, res, next) {
  FaultReport.find(function(err, reports) {
    if (err) return console.error(err);
    var data = [];
    for (var i = 0; i < reports.length; i++) {
      console.log(reports[i]);
      data.push([reports[i].datetime, reports[i].reporter, reports[i].comment[0].comment]);
    }
    res.render('list_all', {
      title: 'Fault List',
      data: data
    });
  });
});

我显然误解了快递/猫鼬的工作方式,非常感谢你能给予的任何帮助或建议。我究竟做错了什么?我需要做什么?

1 个答案:

答案 0 :(得分:0)

感谢Neil Lunn的评论,我发现了我对MongoDB和Mongoose如何工作的误解。

我已经删除了comment.js,并将该代码移至faultreport.js,现在看起来像这样:

var mongoose = require('mongoose');

var Schema = mongoose.Schema;

var CommentSchema = new Schema(
  {
    commenter: {type: String, required: true, max: 128},
    comment: {type: String, required: true, max: 1024},
    datetime: {type: Date, required: true},
  }
);

CommentSchema.methods.getCommenter = function() {
  return this.commenter;
}

CommentSchema.methods.getComment = function() {
  return this.comment;
}

CommentSchema.methods.getDateTime = function() {
  return this.datetime;
}

var FaultReportSchema = new Schema(
  {
    reporter: {type: String, required: true, max: 128},
    comment: [CommentSchema],
    status: {type: String, default: 'Reported', max: 64},
    datetime: {type: Date, required: true},
  }
);

FaultReportSchema.methods.getReporter = function () {
  return this.reporter;
}

FaultReportSchema.methods.getCommentId = function () {
  return this.comment;
}

FaultReportSchema.methods.getStatus = function () {
  return this.status;
}

FaultReportSchema.methods.getDateTime = function () {
  return this.datetime;
}

module.exports = {
  'FaultReport': mongoose.model('FaultReport', FaultReportSchema),
  'FaultComment': mongoose.model('FaultComment', CommentSchema)
};

Express的路由器如下所示:

router.get('/list_all', function(req, res, next) {
  FaultReport.find(function(err, reports) {
    if (err) return console.error(err);
    var data = [];
    for (var i = 0; i < reports.length; i++) {
      console.log(reports[i]);
      data.push([reports[i].datetime, reports[i].reporter, reports[i].comment[0].getComment()]);
    }
    res.render('list_all', {
      title: 'Fault List',
      data: data
    });
  });
});

虽然我确信有更好的方法可以做到这一点,但至少我对我的问题有了答案:)

由于这是由于Neil Lunn的评论,我希望他得到赞誉,但我不确定这里的礼节。我应该接受自己的答案吗?