我正在构建一个webapp,用户可以登录并展示他们的产品(工作室监视器)并向其他用户的产品添加评论。每个用户都有一个带有导航标签的仪表板,他们可以访问他们添加的产品以及他们所做的评论。第一个标签是'用户监控'标签和第二个标签是用户评论'标签。
问题在于,当我制作Monitor.find({user:req.params.userId})时,它会在第一个标签中展示用户添加的所有显示器,但在评论标签中,您只会看到仅针对您添加的监视器。我希望审核标签显示用户进行审核的所有显示器(如果显示器是由其他用户添加的,则无关紧要)。
问题简化:
Mongo模特:
Monitors.js
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
//Create Monitors Schema
const MonitorSchema = new Schema({
model: {
type: String,
required: true
},
company: {
type: String,
required: true
},
description: {
type: String,
required: true
},
imgurl: {
type: String,
},
user: {
type: Schema.Types.ObjectId,
ref: 'users'
},
reviews: [{
reviewBody: {
type: String,
required: true
},
reviewDate: {
type: Date,
default: Date.now
},
reviewUser: {
type: Schema.Types.ObjectId,
ref: 'users'
}
}],
date: {
type: Date,
default: Date.now
}
});
mongoose.model('monitors', MonitorSchema)

user.js的
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const UserSchema = new Schema({
username: String,
password: String,
email: String
})
mongoose.model('users', UserSchema)

路线: dashboard.js
const express = require('express');
const router = express.Router();
const mongoose = require('mongoose');
const async = require('async');
//Load Monitor model
require('../models/Monitors');
const Monitor = mongoose.model('monitors');
//Get Dashboard my monitors tab
router.get('/', function(req, res, next) {
//Check if user is logged in
if(req.isAuthenticated()) {
return next();
}
res.redirect('/')
}, (req, res) => {
Monitor.find({
user:req.user.id
})
.populate('user')
.populate('reviews.reviewUser')
.then(monitors => {
res.render('./dashboard/dashboard', {
monitors: monitors
});
});
})

dashboard.handlebars
<div class="container content-block">
<div class="row">
<div class="col">
<h4 class="mt-5">{{user.username}}s Dashboard</h4>
<ul class="nav nav-tabs" role="tablist">
<li class="nav-item">
<a class="nav-link" id="my-monitors-tab" data-toggle="tab" href="#myMonitors" role="tab" aria-controls="myMonitors" aria-selected="true">My Monitors</a>
</li>
<li class="nav-item">
<a class="nav-link" id="my-reviews-tab" data-toggle="tab" href="#myReviews" role="tab" aria-controls="myReviews" aria-selected="false">My Reviews</a>
</li>
</ul>
<div class="tab-content">
<div class="tab-pane fade show active" id="myMonitors" role="tabpanel" aria-labelledby="my-monitors-tab">
<div class="card-columns dashboard_monitors content-block">
{{#each monitors}}
<div class="card dashboard_monitors_element p-4">
{{#if imgurl}}
<a href="/monitors/show/{{id}}">
<img src="{{imgurl}}" alt="{{company}} {{model}}" class="card-img-top img-fluid">
</a> {{/if}}
<div class="card-body">
<a href="/monitors/show/{{id}}">
<h5 class="card-title mt-3">
{{company}} {{model}}
</h5>
</a>
<p class="card-text">
{{#if description}} {{truncate description 200}} {{/if}}
<a href="/monitors/show/{{id}}">Read more</a>
<hr class="my-2"> Added by
<a href="/monitors/user/{{user.id}}">{{user.username}}</a> on {{formatDate date 'YYYY-MM-DD'}}
</p>
<hr class="my-4">
<div class="mx-auto">
{{#if user}} {{{edit user.id ../user.id id}}} {{/if}}
</div>
</div>
</div>
{{else}}
<h1>No monitors to display</h1>
{{/each}}
</div>
</div>
<div class="tab-pane fade" id="myReviews" role="tabpanel" aria-labelledby="my-reviews-tab">
{{#each monitors}}
{{#each reviews}}
{{reviewBody}}
{{/each}}
{{/each}}
</div>
</div>
</div>
</div>
</div>
</div>
&#13;