以Pug形式显示来自嵌套查询NodeJS的数据

时间:2018-11-11 07:31:04

标签: javascript node.js mongodb pug mongoose-schema

我正在尝试建立一个门户,用户可以在其中上传照片和相应的披露信息(具有有关上传照片的详细信息)。用户将发送披露信息和照片以供审查。该评论是一个如下的架构:

router.get('/reviews', function(req, res) {
  var reviewMap = {};
  var disclosureMap = {};
  var photoMap = {};

  Review.find({}, function(err, reviews) {
    if(err) throw err;

    reviews.forEach(function(review) {    // Iterate over all reviews

      reviewMap[review._id] = review;
      Disclosure.findById(review.disc_id, function(err, disclosure){  // Find disclosure id of that review
        if(err) throw err;
        // console.log(disclosure)  // This prints the whole disclosure properly
        disclosureMap[review._id] = disclosure;
      });

      Photo.find({
        '_id': { $in: review.doc_id
              }
        }, function(err, docs){
          //console.log(docs);            // This prints out details about the pics properly
          photoMap[review._id] = docs;
        });
    });
    //console.log(reviewMap);
    for(var index in reviewMap) {
      console.log(index);
      console.log(disclosureMap[index]);
      console.log(photoMap[index]);
      console.log("Next");
    }
    res.render('reviews', { title: 'Photo Reviews', msg:req.query.msg,
      reviewlist: reviewMap})  ;
  });
});

此打印:

5be69b9ef365292b0ccf4067
undefined
undefined
Next
(node:4216) [DEP0079] DeprecationWarning: Custom inspection function on Objects
via .inspect() is deprecated

评论模式为:

let reviewSchema = mongoose.Schema({

  ts:  { type: String },

  disc_id: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'Disclosure',
  },
  doc_id: [{
    type: mongoose.Schema.Types.ObjectId,
    ref: 'Photos',
  }],

});

reviewMap是:

{ '5be69b9ef365292b0ccf4067':
   { _id: 5be69b9ef365292b0ccf4067,
     disc_id: 5be576add1c7b62438c804c7,
     ts: 'Sat Nov 10 2018 14:19:34 GMT+0530 (India Standard Time)',
     __v: 0,
     doc_id: [ 5be536fcb01fac1c4c94ae87, 5be536d0b01fac1c4c94ae86 ] } }

我的问题是如何在帕格表单(res.render('review'))中显示评论ID,披露ID和为此评论上传的照片。如果我仅直接通过reviewList,则无法仅使用Pug表单中的doc_id显示图片。如果有一种方法可以只使用模式的ObjectID来显示嵌套模式的数据,那也很好。

能给我指出正确的方向吗?

谢谢, 般若

0 个答案:

没有答案