[英]Access documents of linked collections MongoDB Nodejs Handlebars
我是一名业余 web 开发人员,我正在尝试一个创建错误跟踪平台的项目。 下面是我的仪表板的屏幕截图。 标题为“报告者”的列显示了报告错误的用户的用户 ID。 我的目标是不显示用户 ID,而是显示用户名。
我尝试了几次尝试,但一直碰壁。 我使用的相关技术有Nodejs、Express、Handlebars和MongoDB
我有两个 collections; 用户和票。 我已经在 Ticket 集合中链接了 User 集合。
用户收藏
const mongoose = require('mongoose')
const UserSchema = new mongoose.Schema({
name: {
type: String,
required: true
},
email: {
type: String,
},
password: {
type: String
},
userType: {
type: String,
required: true,
default: 'Tester',
enum: ['Tester', 'Admin']
}
}, {timestamps: true})
const User = mongoose.model('User', UserSchema);
module.exports = User;
取票
const mongoose = require('mongoose')
const TicketSchema = new mongoose.Schema({
title: {
type: String,
trim: true
},
tType: {
type: String,
enum: ['Development', 'Testing', 'Production']
},
status: {
type: String,
default: 'Open',
enum: ['Open', 'Resolved', 'Closed']
},
priority: {
type: String,
enum: ['Low', 'Medium', 'High']
},
assignedTo: {
type: String,
enum: ['Julian', 'Jeremiah', 'Madani', 'Yassine', 'Nate']
},
description: {
type: String,
},
createdAt: {
type: Date,
default: Date.now
},
user: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User'
},
}, {timestamps: true})
const Ticket = mongoose.model('Ticket', TicketSchema);
module.exports = Ticket;
我的索引页面
const express = require('express')
const router = express.Router();
const { ensureAuth, ensureGuest } = require('../config/auth');
const Ticket = require('../models/Ticket');
const User = require('../models/User');
// Dashboard page
router.get('/dashboard', ensureAuth, async (req, res) => {
try {
const tickets = await Ticket.find({ user: req.user.id }).lean()
const userName = await User.find({}, {_id: 1, name: 1}).lean() // Me trying to access the User collection
if(tickets.user == userName._id) {
res.render('dashboard.hbs', {
name: req.user.name,
userType: req.user.userType,
tickets,
userName
})
}
console.log(userName)
} catch (err) {
console.log(err)
res.render('error/500.hbs')
}
})
module.exports = router;
最后,我的车把模板
{{#if tickets}}
{{!-- {{#ifEquals tickets.user userName._id}} --}}
{{#ifEquals userType "Admin"}}
<table class="table table-hover">
<thead>
<tr class="table-active">
<th scope="col">Ticket Number/ID</th>
<th scope="col">Ticket Title</th>
<th scope="col">Status</th>
<th scope="col">Priority</th>
<th scope="col">Reported by</th>
<th scope="col">Assigned</th>
<th scope="col">When</th>
{{!-- <th scope="col"></th>
<th scope="col"></th> --}}
</tr>
</thead>
<tbody>
{{#each tickets}}
<tr class="taable-default">
<th scope="row"><a href="/tickets/{{_id}}">{{_id}}</a></th>
<td>{{title}}</td>
<td><span class="dash-status">{{status}}</span></td>
<td>{{priority}}</td>
<td>{{user}}</td>
<td>{{assignedTo}}</td>
<td>{{formatDate createdAt 'MMMM Do YYYY, h:mm:ss a'}}</td>
{{!-- <td></td>
<td></td> --}}
</tr>
{{/each}}
</tbody>
</table>
...
...
首先,我使用 mongoose 方法findById()
使用它的 Objectid 搜索 mongodb 文档。 之后,我添加了user
变量以使用dashboard.hbs
呈现它
const express = require('express')
const router = express.Router();
const { ensureAuth, ensureGuest } = require('../config/auth');
const Ticket = require('../models/Ticket');
const User = require('../models/User');
// Dashboard page
router.get('/dashboard', ensureAuth, async (req, res) => {
try {
const tickets = await Ticket.find({ user: req.user.id }).lean()
const user = await User.findById(tickets.user).lean() // Me trying to access the User collection
if(tickets.user == user._id) {
return res.render('dashboard.hbs', { // I would return here
name: req.user.name,
userType: req.user.userType,
tickets,
user
})
}
} catch (err) {
console.log(err)
return res.render('error/500.hbs') // I would return here
}
})
module.exports = router;
您需要在您的车把文件中更改的是简单地请求user.name
,我们之前确定它来呈现dashboard.hbs
模板:
{{#if tickets}}
{{!-- {{#ifEquals tickets.user user._id}} --}}
{{#ifEquals userType "Admin"}}
<table class="table table-hover">
<thead>
<tr class="table-active">
<th scope="col">Ticket Number/ID</th>
<th scope="col">Ticket Title</th>
<th scope="col">Status</th>
<th scope="col">Priority</th>
<th scope="col">Reported by</th>
<th scope="col">Assigned</th>
<th scope="col">When</th>
{{!-- <th scope="col"></th>
<th scope="col"></th> --}}
</tr>
</thead>
<tbody>
{{#each tickets}}
<tr class="taable-default">
<th scope="row"><a href="/tickets/{{_id}}">{{_id}}</a></th>
<td>{{title}}</td>
<td><span class="dash-status">{{status}}</span></td>
<td>{{priority}}</td>
<td>{{../user.name}}</td>
<td>{{assignedTo}}</td>
<td>{{formatDate createdAt 'MMMM Do YYYY, h:mm:ss a'}}</td>
{{!-- <td></td>
<td></td> --}}
</tr>
{{/each}}
</tbody>
</table>
...
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.