【发布时间】:2020-08-25 02:24:04
【问题描述】:
I have 3 collections in mongoose. one is campaign collection that reference to user and other one is photo collection that reference to campaign collection. I can pull the campaigns data based on user but not able fetch photos based on campaigns. Here One user might have many campaigns and each campaign has their own photo collections.
My models :
**Photo collection:**
var photoSchema = new Schema({
Name: {type: String},
Email: { type: String },
Photo: { type: String },
Description:{type:String},
PhoneNumber:{type:String},
CampaignId: {
type: Schema.Types.ObjectId,
ref: 'Campaigns',
}
});
这是引用用户的活动集合。这再次从照片收藏中获得参考。我想在画廊中呈现与一项活动相关的照片。广告系列标题作为 URL 中的参数发送,但无法弄清楚如何发送。
照片收藏的发布路线:
exports.postEntriesCollection = (req, res) => {
Campaign.findOne({Title:req.params.Title}, function(err, campaign) {
let filename = '';
if(!isEmpty(req.files)) {
let file = req.files.file;
filename = file.name;
let uploadDir = './public/PhotoContestUploads/uploads/';
console.log(filename);
file.mv(uploadDir+filename, (err) => {
if (err)
throw err;
});
}
const photo = new PhotoEntries({
Email:req.body.email,
Name:req.body.name,
Description:req.body.description,
Phonenumber:req.body.Phone,
CampaignId:campaign._id,
Photo:`uploads/${filename}`
});
photo.save().then(post => {
req.flash('success', { msg: 'Uploaded successfully' });
res.redirect('#');
});
});
}
**campaign collection:**
var campaignSchema = new Schema({
Campname: {type: String},
userId: {
type: Schema.Types.ObjectId,
ref: 'User',
}
});
****To render gallery my route like this :** I try to pull photos using campaign ID stored in photos collection here :**
exports.getGallery = (req, res) => {
PhotoEntries.find({req.CampaignId}, ['Photo'], {sort:{ _id: -1} }, function(err, photos) {
res.render('admin/gallery', { photolist : photos });
});
}
I am not able to get gallery of particular campaign instead i am getting all photos relating all campaigns. I want to show only gallery of particular campaign. I have gallery url like this : /photocontestapp/awesome%20baby%20srija/gallery.How can i achieve this.
【问题讨论】:
-
我假设
PhotoEnties是photoSchema的模型,{req.CampaignId}应该是{CampaignId: req.CampaignId},对吧? -
是的,正确的@TheeSritabtim
-
在这种情况下,您应该检查
req.CampaignId不是undefined并且是架构中指定的ObjectId类型。 -
好吧,我将删除它,我如何获取特定活动的画廊照片
-
我正在将广告系列 ID 保存到照片架构中以供参考