所以我有两个模式,一个PoemRegistrations和其他的CompetitionResults我想在一个页面中呈现两个表单。
当我渲染大部分PoemRegistrations字段和其中一个CompetitionResults时,我试图填充PoemRegistrations以从CompetitionResults winnersName字段中提取数据,但我遇到了一些问题。
这是我到目前为止所尝试的内容。
模式
winnersName: [{ type: mongoose.Schema.Types.ObjectId, ref: 'CompetitionResults' }],
在我正在尝试的路线中
router.get('/dashboard/all-poems', ensureAuthenticated, (req, res) => {
PoemRegistrations.find({}).
populate('winnersName').
exec(function(err, poemRegistrations) {
res.render('dashboard/all-poems.hbs', {
pageTitle: 'All Poems',
poemRegistrations: poemRegistrations
});
});
});
我希望能够通过
来理想地渲染winnersName{{#each poemRegistrations}}
<tr>
<td>{{winnersName}}</td>
</tr>
{{/each}}
我希望能够从另一个模式中拉入一个字段,将其从一个模式拉到另一个模式并渲染到页面。
我可以在没有问题的情况下拉入PoemRegistration字段,我只是想让winnersName呈现。
这里的任何帮助都会很棒。
架构[诗注册]
const express = require('express');
const mongoose = require('mongoose');
var app = express();
if (app.get('env') === 'production') {
mongoose.connect(process.env.MONGODB_URI, { useMongoClient: true });
} else {
mongoose.connect('mongodb://localhost/pol-development', { useMongoClient: true });
}
var db = mongoose.connection;
mongoose.Promise = global.Promise;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
console.log("Connection has been established");
});
var PoemRegistrationsSchema = mongoose.Schema({
schoolName: String,
competitionDate: String,
// Poem 1
poem1AuthorName: String,
poem1Title: String,
poem1Url: String,
poem1AnthologyUrl: String,
poem1LinesCheck: Boolean,
poem1CenturyCheck: Boolean,
poem1FreeCheck: Boolean,
// Poem 2
poem2AuthorName: String,
poem2Title: String,
poem2Url: String,
poem2AnthologyUrl: String,
poem2LinesCheck: Boolean,
poem2CenturyCheck: Boolean,
poem2FreeCheck: Boolean,
// Poem 3
poem3AuthorName: String,
poem3Title: String,
poem3Url: String,
poem3AnthologyUrl: String,
poem3LinesCheck: Boolean,
poem3CenturyCheck: Boolean,
poem3FreeCheck: Boolean,
winnersName: [{ type: mongoose.Schema.Types.ObjectId, ref: 'CompetitionResults' }],
// admin fields
poemRegistrationRequiredDocuments: Boolean
});
var PoemRegistrations = module.exports = mongoose.model('PoemRegistrations', PoemRegistrationsSchema);
架构[竞争结果]
const express = require('express');
const mongoose = require('mongoose');
var app = express();
if (app.get('env') === 'production') {
mongoose.connect(process.env.MONGODB_URI, { useMongoClient: true });
} else {
mongoose.connect('mongodb://localhost/pol-development', { useMongoClient: true });
}
var db = mongoose.connection;
mongoose.Promise = global.Promise;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
console.log("Connection has been established");
});
var CompetitionResultsSchema = mongoose.Schema({
schoolName: String,
winnersName: String,
winnersGrade: String,
winnersAddress: String,
winnersCity: String,
winnersZip: String,
competitionDate: String,
winnersTelephone: String,
winnersParentName: String,
winnersParentTelephone: String,
winnersTShirtSize: String,
winnersAccommodation: Boolean,
winnersAccommodationComments: String,
// admin fields
winnersAttendedRehersal: Boolean,
winnersAttendedMainCompetition: Boolean,
winnersReleaseForm: Boolean,
runnerUpsName: String,
runnerUpsGrade: String,
runnerUpsAddress: String,
runnerUpsCity: String,
runnerUpsZip: String,
runnerUpsTelephone: String,
runnerUpsParentName: String,
runnerUpsParentTelephone: String,
runnerUpsTShirtSize: String,
runnerUpsAccommodation: Boolean,
runnerUpsAccommodationComments: String,
// admin comments
runnerUpsAttendedRehersal: Boolean,
runnerUpsAttendedMainCompetition: Boolean,
runnerUpsReleaseForm: Boolean
});
var CompetitionResults = module.exports = mongoose.model('CompetitionResults', CompetitionResultsSchema);
答案 0 :(得分:0)
更好的方法是重新设计您的模式,如下所示:
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
var poemSchema = Schema({
authorName: String,
title: String,
url: String,
anthologyUrl: String,
linesCheck: Boolean,
centuryCheck: Boolean,
freeCheck: Boolean
});
var competitionResultSchema = Schema({
schoolName: String,
name: String,
grade: String,
address: String,
city: String,
zip: String,
competitionDate: String,
telephone: String,
parentName: String,
parentTelephone: String,
tShirtSize: String,
accommodation: Boolean,
accommodationComments: String,
// admin fields
attendedRehersal: Boolean,
attendedMainCompetition: Boolean,
releaseForm: Boolean,
});
var poemRegistrationSchema = mongoose.Schema({
schoolName: String,
competitionDate: String,
// poems fields
poems: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Poem' }],
// competition results fields
winner: { type: mongoose.Schema.Types.ObjectId, ref: 'CompetitionResult' },
runnerUp: { type: mongoose.Schema.Types.ObjectId, ref: 'CompetitionResult' },
// admin fields
poemRegistrationRequiredDocuments: Boolean
});
var CompetitionResults = mongoose.model('CompetitionResult', competitionResultSchema);
var Poem = mongoose.model('Poem', poemSchema);
var PoemRegistration = mongoose.model('PoemRegistration', poemRegistrationSchema);
创建poemRegistration
数据时,您还需要poems
,winner
和runnerUp
数据。例如,当您使用异步库的waterfall工作流创建3首诗时:
var workflowPipeline = [
function(callback) {
Poem.insertMany([
{
authorName: "author1",
title: "foo1",
url: "foo/bar",
anthologyUrl: "foo/bar",
linesCheck: false,
centuryCheck: false,
freeCheck: false
},
{
authorName: "author2",
title: "foo2",
url: "foo/bar2",
anthologyUrl: "foo/bar2",
linesCheck: false,
centuryCheck: false,
freeCheck: false
},
{
authorName: "author3",
title: "foo3",
url: "foo/bar3",
anthologyUrl: "foo/bar3",
linesCheck: false,
centuryCheck: false,
freeCheck: false
}
], function(err, poems) {
if (err) {
callback(err, null);
} else {
callback(null, poems);
}
});
},
function(poems, callback) {
var winner = new CompetitionResult({ schoolName: 'School 1', name: 'Result 1' });
var runnerUp = new CompetitionResult({ schoolName: 'School 2', name: 'Result 2' });
callback(null, { poems: poems, winner: winner, runnerUp: runnerUp });
},
function(data, callback) {
var registration = new PoemRegistration({
schoolName: 'School 3',
competitionDate: "2018-02-14",
poems: data.poems.map(p => p._id),
winner: data.winner._id,
runnerUp: data.runnerUp._id
});
registration.save(function(err, reg){
if (err) return callback(err, null);
callback(null, reg);
});
}
];
async.waterfall(workflowPipeline, function (err, result) {
// result is the new poem registration
});