我有一个正在构建的简单的帮助台应用程序,用户可以在其中请求更改站点。功能之一是能够查看正在运行的特定人员的所有请求。但是,在该页面上,我希望有一个类似于“用户的请求”的内容,其中user是您所在的人员页面。但是,如果没有一些奇怪的问题,我似乎无法使其正常工作。如果我使用:
{{#each request}}
{{user.firstName}}'s Request
{{/each}}
它可以工作,但最终我将头写入用户请求的次数。但是,当我尝试:
{{request.user.firstName}}
它什么也不返回。
我的路线正在填充用户数据,所以我认为我应该能够直接引用它。这是路线:
// list Request by User
router.get('/user/:userId', (req, res) => {
Request.find({user: req.params.userId})
.populate('user')
.populate('request')
.then(request => {
res.render('request/user', {
request: request,
});
});
});
以下是架构:
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
// Create Schema
const RequestSchema = new Schema({
title: {
type: String,
required: true,
},
body: {
type: String,
required: true,
},
status: {
type: String,
default: 'new',
},
priority: {
type: String,
default: 'low',
},
project: {
type: String,
default: 'miscellaneous',
},
category: {
type: String,
default: 'change',
category: ['change', 'bug', 'enhancement', 'investigation', 'minor_task', 'major_task', 'question'],
},
organization: {
type: String,
default: 'any',
},
assignedUser: {
type: String,
default: 'venkat',
},
allowComments: {
type: Boolean,
default: true,
},
user: {
type: Schema.Types.ObjectId,
ref: 'users',
},
lastUser: {
type: Schema.Types.ObjectId,
ref: 'users',
},
date: {
type: Date,
default: Date.now,
},
lastUpdate: {
type: Date,
default: Date.now,
},
comments: [{
commentBody: {
type: String,
required: true,
},
commentDate: {
type: Date,
default: Date.now,
},
commentUser: {
type: Schema.Types.ObjectId,
ref: 'users',
},
}],
});
// Create collection and add Schema
mongoose.model('request', RequestSchema);
答案 0 :(得分:0)
如果有人想知道如何做,答案是将数组标识符添加到点路径符号中:
<h4>{{request.0.user.firstName}}'s Request</h4>