Виртуальный объект неправильно связывается со схемой документа (Пн goose) - PullRequest
0 голосов
/ 08 апреля 2020

У меня есть две схемы документа, и я пытаюсь связать их, используя «виртуальный», чтобы документ A мог иметь документ B, а затем отобразить его соответствующим образом с помощью рулей, например: {{#each documentA.documentB}}, обе схемы заполняют базу данных правильно. Любая помощь будет принята с благодарностью, я думаю, что в контроллере документа А не должно быть ничего дополнительного, поэтому я не включил ее, но если я ошибаюсь, пожалуйста, дайте мне знать, и я обновлю соответствующим образом. Спасибо.

Документ A Модель (с виртуальным):

const mongoose = require('mongoose');
const validator = require('validator');
mongoose.Promise = global.Promise;
const mongodbErrorHandler = require('mongoose-mongodb-errors');
const passportLocalMongoose = require('passport-local-mongoose');
const teamSchema = new mongoose.Schema({
  teamName:
  {
    type: String,
    trim: true
  },
  addr1: {
    type: String,
    trim: true
  },
  addr2: {
    type: String,
    trim: true
  },
  teamType: {
    type: String
  },
  county: {
    type: String 
}, 
  phNum: {
    type: String 
}, 
  email: {
    type: String 
}, 
  teamType: {
    type: String 
}, 
  coach: {
    type: mongoose.Schema.ObjectId, 
    ref: 'User'
}, 
  teamCode: 
  {
    type:String
}, 
  col1:
  {
  type: String
}, 
  col2: 
  {
  type: String 
}, 
  jerseyType:
  {
  type: String 
}
});


teamSchema.virtual('videos', {
  ref: 'Video', 
  localField: '_id', 
  foreignField: 'team', 
});

module.exports = mongoose.model('Team', teamSchema);

Документ B Модель:

const mongoose = require('mongoose');

mongoose.Promise = global.Promise;

const videoSchema = new mongoose.Schema ( { 
team : { 
    type: mongoose.Schema.ObjectId,
    ref: 'Team',
    required: true,
    }, 
code : {
    type: String, 
    trim: true,
    required: 'Code cannot be Empty!',
    },
title : {
    type: String, 
    trim: true,
    required: 'Title Cannot be Empty!',
    }, 
created : {
    type: Date, 
    default: Date.now, 
    }, 
}); 

module.exports = mongoose.model('Video', videoSchema); 

Контроллер документа B:

const mongoose = require('mongoose'); 

const Video = mongoose.model('Video');

exports.addVideo = async (req, res) => {

  await (new Video(req.body)).save();

  res.redirect('/dashboard/video');
};

exports.getVideoAnalysisPage = (req, res) => 
{
res.render('dashboard/video.hbs',{ title: "Video", cybersecurity: `${req.csrfToken()}`}); 
};

Ссылка в файле Handlebars:

{{#each team.videos}}
<tr>
<td class="vidThumb"></td>
<td class="vidInfo"><b><span style="font-size: 20px">{{this.title}}</span></b><br><span style="font-size: 12px">Uploaded: {{this.created}}</span></td>
</tr>
{{/each}}
...